-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalin_v2.cpp
More file actions
57 lines (55 loc) · 1.28 KB
/
palin_v2.cpp
File metadata and controls
57 lines (55 loc) · 1.28 KB
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
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
int main(int argc, char*argv[])
{
int node [MAX+2][26];
memset(&node, 0, sizeof(int)*26*(MAX+2));
int suffix_link [MAX+2]={0}; // Contains index of node
int len[MAX+2]={0};
int N;
cin >> N;
string s;
cin >> s;
//Setup imaginary and 0-node
int count =0;
suffix_link[count] = 0; // suffix link of imaginary points to itself
len [count] = -1;
count++;
suffix_link[count] = 0;
len[count] = 0;
count++;
int current_node = 0;
int max_pal = 0;
for (int i =0; i< s.size(); i++)
{
//cout<< i<<" "<<max_pal<<endl;
if( len[current_node]==-1)
{
node[0][s[i]-97] = i+2;
len[count] = 1;
suffix_link [count]=1;
}
else
{
int n = current_node;
while((len[n]!=-1) && (s[i] != s[i-len[n]-1]) )
n = suffix_link[n];
// Suffix Link
int l = suffix_link[n];
while((len[l] !=-1) && (s[i] != s[i-len[l]-1]))
l = suffix_link[l];
//suffix_link[count] = node[l][s[i]-97];
suffix_link[count] = node[l][s[i]-97]?node[l][s[i]-97]:1;
//Update the node
node [n][s[i]-97] = i+2;
len[count] = 2 + len[n];
}
if(len[count] > max_pal)
max_pal = len[count];
current_node = count;
count++;
}
cout << max_pal;
return 0;
}