File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 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 L ;
26+ static char [] A ;
27+ static int [] X ;
28+
29+ public static void main(String [] args) throws Exception {
30+
31+ ready();
32+ solve();
33+
34+ bwEnd();
35+
36+ }
37+
38+ static void ready() throws Exception {
39+
40+ L = nextInt();
41+ X = new int [L ];
42+ A = br. readLine(). toCharArray();
43+
44+ }
45+
46+ static void solve() throws Exception {
47+
48+ X [0 ] = 0 ;
49+ for (int i= 1 ;i< L ;i++ ) {
50+ int j = X [i- 1 ];
51+ while (j> 0 && A [j] != A [i]) j = X [j- 1 ];
52+ X [i] = j + (A [j] == A [i] ? 1 : 0 );
53+ }
54+
55+ bw. write((L - X [L - 1 ]) + " \n " );
56+
57+ }
58+
59+ }
60+
61+ ```
You can’t perform that action at this time.
0 commit comments