[Link] https://www.acmicpc.net/problem/4354
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
while(!s.equals(".")) {
sb.append(kmp(s)).append("\n");
s = br.readLine();
}
print(sb);
}
static int kmp(String p) {
int l = 0, len = p.length();
int[] kmp = new int[p.length()];
for(int r = 1; r < len; r++) {
while(l > 0 && p.charAt(l) != p.charAt(r)) l = kmp[l - 1];
if(p.charAt(l) == p.charAt(r)) kmp[r] = ++l;
}
int chunkLen = len - kmp[len - 1], max = len / chunkLen;
return len - max * chunkLen == 0 ? max : 1;
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}