Skip to content

Commit efe978d

Browse files
authored
Merge pull request #1384 from AlgorithmWithGod/0224LJH
[20251112] BOJ / G3 / 알 수 없는 문장 / 이종환
2 parents 2c93efc + 702d805 commit efe978d

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.io.*;
6+
import java.util.*;
7+
8+
public class Main {
9+
10+
11+
static char[] arr;
12+
static int[] dp;
13+
static int cnt,ans,len;
14+
15+
static final int CHAR_LEN = 'z'-'a'+1;
16+
static final int MAX = Integer.MAX_VALUE/2;
17+
static final int X = -1;
18+
19+
static HashSet<Word> words = new HashSet<>();
20+
21+
static class Word{
22+
//각 알파벳 몇번 사용했는지 카운트
23+
int[] count = new int[CHAR_LEN];
24+
char[] content;
25+
26+
public Word(String w) {
27+
content = w.toCharArray();
28+
for (int i = 0; i < content.length; i++) {
29+
count[content[i]-'a']++;
30+
}
31+
}
32+
33+
public int getCost(int idx) {
34+
int end = idx+ content.length;
35+
if (arr.length < end) {
36+
return X;
37+
}
38+
39+
int[] targetCnt = new int[CHAR_LEN];
40+
for (int i = idx; i < end; i++) {
41+
targetCnt[arr[i]-'a']++;
42+
}
43+
44+
for (int i = 0; i< CHAR_LEN; i++) {
45+
if (targetCnt[i] != count[i]) return X;
46+
}
47+
48+
int cost = 0;
49+
50+
for (int i = 0; i < content.length; i++) {
51+
if (arr[i+idx] != content[i])cost++;
52+
}
53+
54+
55+
56+
return cost;
57+
}
58+
59+
}
60+
61+
62+
public static void main (String[] args) throws NumberFormatException, IOException {
63+
init();
64+
65+
process();
66+
print();
67+
68+
}
69+
70+
public static void init() throws IOException {
71+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
72+
arr = br.readLine().toCharArray();
73+
cnt = Integer.parseInt(br.readLine());
74+
ans = MAX;
75+
len = arr.length;
76+
dp = new int[len+1];
77+
78+
79+
Arrays.fill(dp,MAX);
80+
dp[0] = 0;
81+
82+
for (int i = 0; i < cnt; i++) {
83+
String word = br.readLine();
84+
words.add(new Word(word));
85+
}
86+
87+
}
88+
89+
public static void process() {
90+
for (int i = 0; i < len; i++) {
91+
for (Word w: words) {
92+
int cost = w.getCost(i);
93+
if (cost == X) continue;
94+
95+
dp[i+w.content.length] = Math.min(dp[i+w.content.length], dp[i]+cost);
96+
}
97+
}
98+
}
99+
100+
public static void print() {
101+
System.out.println(dp[len]==MAX?-1:dp[len]);
102+
}
103+
104+
}
105+
106+
```

0 commit comments

Comments
 (0)