-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileRecoverTesting.java
48 lines (45 loc) · 1.01 KB
/
FileRecoverTesting.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** #kmp */
import java.util.Scanner;
class FileRecoverTesting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int k = sc.nextInt();
String s = sc.next();
if (k == -1 && s.equals("*")) break;
int result = 0;
int n = s.length();
if (k >= n) {
// build prefix array
int[] prefix = new int[n];
calculatePrefix(s, prefix);
int cnt = prefix[n - 1];
if (cnt == 0) {
result = k / n;
} else {
result = 1 + (k - n) / (n - cnt);
}
}
System.out.println(result);
}
}
public static void calculatePrefix(String p, int[] prefix) {
prefix[0] = 0;
int m = p.length();
int j = 0, i = 1;
while (i < m) {
if (p.charAt(i) == p.charAt(j)) {
j++;
prefix[i] = j;
i++;
} else {
if (j != 0) {
j = prefix[j - 1];
} else {
prefix[i] = 0;
i++;
}
}
}
}
}