File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ #define MAX 100000
4
+ int main (int argc, char *argv[])
5
+ {
6
+ int node [MAX+2 ][26 ];
7
+ memset (&node, 0 , sizeof (int )*26 *(MAX+2 ));
8
+ int suffix_link [MAX+2 ]={0 }; // Contains index of node
9
+ int len[MAX+2 ]={0 };
10
+ int N;
11
+ cin >> N;
12
+ string s;
13
+ cin >> s;
14
+ // Setup imaginary and 0-node
15
+ int count =0 ;
16
+ suffix_link[count] = 0 ; // suffix link of imaginary points to itself
17
+ len [count] = -1 ;
18
+ count++;
19
+
20
+ suffix_link[count] = 0 ;
21
+ len[count] = 0 ;
22
+ count++;
23
+ int current_node = 0 ;
24
+ int max_pal = 0 ;
25
+ for (int i =0 ; i< s.size (); i++)
26
+ {
27
+ // cout<< i<<" "<<max_pal<<endl;
28
+ if ( len[current_node]==-1 )
29
+ {
30
+ node[0 ][s[i]-97 ] = i+2 ;
31
+ len[count] = 1 ;
32
+ suffix_link [count]=1 ;
33
+ }
34
+ else
35
+ {
36
+ int n = current_node;
37
+ while ((len[n]!=-1 ) && (s[i] != s[i-len[n]-1 ]) )
38
+ n = suffix_link[n];
39
+
40
+ // Suffix Link
41
+ int l = suffix_link[n];
42
+ while ((len[l] !=-1 ) && (s[i] != s[i-len[l]-1 ]))
43
+ l = suffix_link[l];
44
+ // suffix_link[count] = node[l][s[i]-97];
45
+ suffix_link[count] = node[l][s[i]-97 ]?node[l][s[i]-97 ]:1 ;
46
+ // Update the node
47
+ node [n][s[i]-97 ] = i+2 ;
48
+ len[count] = 2 + len[n];
49
+ }
50
+ if (len[count] > max_pal)
51
+ max_pal = len[count];
52
+ current_node = count;
53
+ count++;
54
+ }
55
+ cout << max_pal;
56
+ return 0 ;
57
+ }
You can’t perform that action at this time.
0 commit comments