Skip to content

Commit eba0bb3

Browse files
authored
[20250410] BOJ / P2 / 최대 문자열 붙여넣기 / 권혁준
1 parent 89ebf37 commit eba0bb3

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int M;
26+
static char[] T;
27+
static char[][] P;
28+
static int[][] X;
29+
static int[] dp;
30+
31+
public static void main(String[] args) throws Exception {
32+
33+
ready();
34+
solve();
35+
36+
bwEnd();
37+
38+
}
39+
40+
static void ready() throws Exception{
41+
42+
T = nextToken().toCharArray();
43+
dp = new int[T.length];
44+
M = nextInt();
45+
P = new char[M][];
46+
X = new int[M][];
47+
for(int i=0;i<M;i++) {
48+
P[i] = nextToken().toCharArray();
49+
X[i] = new int[P[i].length];
50+
}
51+
52+
}
53+
54+
static void solve() throws Exception{
55+
56+
for(int i=0;i<M;i++) {
57+
for(int j=1;j<P[i].length;j++) {
58+
X[i][j] = X[i][j-1];
59+
while(X[i][j]>0 && P[i][X[i][j]] != P[i][j]) X[i][j] = X[i][X[i][j]-1];
60+
if(P[i][X[i][j]] == P[i][j]) X[i][j]++;
61+
}
62+
}
63+
64+
int[] idx = new int[M];
65+
for(int i=0;i<T.length;i++) {
66+
for(int j=0;j<M;j++) {
67+
while(idx[j]>0 && P[j][idx[j]] != T[i]) idx[j] = X[j][idx[j]-1];
68+
if(P[j][idx[j]] == T[i]) idx[j]++;
69+
if(idx[j] == P[j].length) {
70+
int res = (i >= P[j].length ? dp[i-P[j].length] : 0) + P[j].length;
71+
dp[i] = Math.max(dp[i], res);
72+
idx[j] = X[j][idx[j]-1];
73+
}
74+
}
75+
if(i>0) dp[i] = Math.max(dp[i], dp[i-1]);
76+
}
77+
78+
bw.write(dp[T.length-1] + "\n");
79+
80+
}
81+
82+
}
83+
84+
```

0 commit comments

Comments
 (0)