-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstring-function-calculation.cpp
More file actions
111 lines (101 loc) · 2.49 KB
/
string-function-calculation.cpp
File metadata and controls
111 lines (101 loc) · 2.49 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define pb push_back
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000007
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn=500007;
char T[maxn];
int n,sa[maxn],tsa[maxn],ra[maxn],tra[maxn],cnt[maxn],plcp[maxn],lcp[maxn],previous[maxn];
int fra(int i){
if(i<=n) return ra[i]; else return 0;
}
void sort(int k){
int maxv=max(300,n);
for(int i=0; i<=maxv; ++i) cnt[i]=0;
for(int i=1; i<=n; ++i) ++cnt[fra(sa[i]+k)];
for(int i=0, sum=0, tmp; i<=maxv; ++i){
tmp=sum;
sum+=cnt[i];
cnt[i]=tmp;
}
for(int i=1; i<=n; ++i) tsa[++cnt[fra(sa[i]+k)]]=sa[i];
for(int i=1; i<=n; ++i) sa[i]=tsa[i];
}
void suffixArray(){
for(int i=1; i<=n; ++i){
sa[i]=i;
ra[i]=T[i];
}
for(int k=1; k<n; k*=2){
sort(k); sort(0);
int tmp=1;
tra[sa[1]]=tmp;
for(int i=2; i<=n; ++i){
if(ra[sa[i]]!=ra[sa[i-1]] || fra(sa[i]+k)!=fra(sa[i-1]+k)) ++tmp;
tra[sa[i]]=tmp;
}
for(int i=1; i<=n; ++i) ra[i]=tra[i];
}
previous[sa[1]]=-1;
for(int i=2; i<=n; ++i) previous[sa[i]]=sa[i-1];
int len=0;
for(int i=1; i<=n; ++i){
if(previous[i]==-1){
plcp[i]=0;
continue;
}
while(previous[i]+len<=n && i+len<=n && T[previous[i]+len]==T[i+len]) ++len;
plcp[i]=len;
len=max(len-1,0);
}
for(int i=1; i<=n; ++i) lcp[i]=plcp[sa[i]];
}
int toLeft[maxn], toRight[maxn], mystack[maxn];
ll res;
void solve(){
res=n;
int last=0;
for(int i=2;i<=n; ++i){
while(last>0 && lcp[mystack[last]] >= lcp[i]) --last;
if(last==0) toLeft[i]=1;
else toLeft[i]=mystack[last];
mystack[++last]=i;
}
last=0;
for(int i=n; i>=2; --i){
while(last>0 && lcp[mystack[last]] >= lcp[i]) --last;
if(last==0) toRight[i]=n+1;
else toRight[i]=mystack[last];
mystack[++last]=i;
}
for(int i=1; i<=n; ++i){
int cnt=toRight[i]-toLeft[i];
res=max(res, 1ll*cnt*lcp[i]);
}
cout<<res<<endl;
}
int main(){
freopen("input.txt","r",stdin);
scanf("%s",T+1);
n=strlen(T+1);
suffixArray();
solve();
return 0;
}