-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task3.java
66 lines (57 loc) · 1.66 KB
/
Task3.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Kornilov Nikita, M3102, 09.05.2021 */
package Sem2.Lab13;
import java.io.*;
import java.util.*;
public class Task3 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public static void main(String[] args) {
String fileName = "prefix";
new Task3().run(String.format("%s.in", fileName), String.format("%s.out", fileName));
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
String inputString = br.readLine();
if (inputString != null) {
in = new StringTokenizer(inputString);
}
else {
return null;
}
}
return in.nextToken();
}
public void solve() throws IOException {
char[] s = nextToken().toCharArray();
int[] prefix = new int[s.length + 1];
int i = 1, j = 0;
while (i < s.length) {
if (s[i] == s[j]) {
prefix[i + 1] = j + 1;
i++; j++;
} else {
if (j > 0) {
j = prefix[j];
} else {
prefix[i + 1] = 0;
i++;
}
}
}
for (int k = 0; k < s.length; k++) {
out.print(prefix[k + 1] + " ");
}
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}