-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuffixAutomaton.cpp
More file actions
79 lines (69 loc) · 1.37 KB
/
Copy pathsuffixAutomaton.cpp
File metadata and controls
79 lines (69 loc) · 1.37 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int N=2e6+10,M=1e5+10,INF=0x3f3f3f3f,MOD=1e9+7;
const double EPS=1e-8;
int n,m;
int tot=1,last=1;
struct Node{
int maxlen,fa;
int ch[26];
}no[N];
char str[N];
ll f[N],ans;
int he[N],no[N],ne[N],idx;
void add(int a,int b){
no[idx]=b;
ne[idx]=he[a];
he[a]=idx++;
}
void extend(int c){
int p=last,np=last=++tot;
f[tot]=1;
no[np].maxlen=no[p].maxlen+1;
for(;p&&!no[p].ch[c];p=no[p].fa){
no[p].ch[c]=np;
}
if(!p){
no[np].fa=1;
}
else {
int q=no[p].ch[c];
if(no[q].maxlen==no[p].maxlen+1){
no[np].fa=q;
}
else {
int nq=++tot;
no[nq]=no[q];
no[nq].maxlen=no[p].maxlen+1;
no[q].fa=no[np].fa=nq;
for(;p&&no[p].ch[c]==q;p=no[p].fa){
no[p].ch[c]=nq;
}
}
}
}
void dfs(int u){
for(int i=he[u];~i;i=ne[i]){
int j=no[i];
dfs(j);
f[u]+=f[j];
}
if(f[u]>1){
ans=max(ans,f[u]*no[u].maxlen);
}
}
int main(){
scanf("%s",str);
for(int i=0;str[i];i++){
extend(str[i]-'a');
}
memset(he,-1,sizeof he);
for(int i=2;i<=tot;i++){
add(no[i].fa,i);
}
dfs(1);
printf("%lld",ans);
}