标签:+= cin mat base iostream ems -- 最长子串 ons
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
void ____(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
const int MAXN = 1e5+7;
int n,A[MAXN];
struct SAM{
int len[MAXN],link[MAXN],firstpos[MAXN],lastpos[MAXN],ch[MAXN][180],tot,last,cnt[MAXN],c[MAXN],sa[MAXN];
void init(){ link[tot = last = cnt[0] = len[0] = 0] = -1; memset(ch[0],0,sizeof(ch[0])); }
void extend(int x){
int np = ++tot, p = last; firstpos[tot] = lastpos[tot] = len[tot] = len[last] + 1;
memset(ch[tot],0,sizeof(ch[tot])); cnt[tot] = 1;
while(p!=-1 and !ch[p][x]){
ch[p][x] = np;
p = link[p];
}
if(p==-1) link[np] = 0;
else{
int q = ch[p][x];
if(len[p]+1==len[q]) link[np] = q;
else{
int clone = ++tot;
cnt[clone] = 0;
firstpos[clone] = firstpos[q];
lastpos[clone] = lastpos[q];
len[clone] = len[p] + 1;
for(int i = 0; i < 180; i++) ch[clone][i] = ch[q][i];
link[clone] = link[q];
while(p!=-1 and ch[p][x]==q){
ch[p][x] = clone;
p = link[p];
}
link[np] = link[q] = clone;
}
}
last = np;
}
int solve(){
for(int i = 0; i <= n; i++) c[i] = 0;
for(int i = 0; i <= tot; i++) c[len[i]]++;
for(int i = 1; i <= n; i++) c[i] += c[i-1];
for(int i = tot; i >= 0; i--) sa[c[len[i]]--] = i;
int ret = 0;
for(int i = tot+1; i >= 1; i--){ //这里要注意,基数排序的时候是0~tot,所以排名最后的是tot+1
int u = sa[i];
cnt[link[u]] += cnt[u];
lastpos[link[u]] = max(lastpos[link[u]],lastpos[u]);
ret = max(ret,min(len[u] + 1,lastpos[u]-firstpos[u]));
}
if(ret<5) ret = 0;
return ret;
}
}sam;
void solve(){
for(int i = 1; i <= n; i++) scanf("%d",&A[i]);
for(int i = 1; i < n; i++) A[i] = A[i+1] - A[i] + 88;
sam.init(); for(int i = 1; i < n; i++) sam.extend(A[i]);
printf("%d\n",sam.solve());
}
int main(){
while(scanf("%d",&n)!=EOF and n) solve();
return 0;
}
标签:+= cin mat base iostream ems -- 最长子串 ons
原文地址:https://www.cnblogs.com/kikokiko/p/12700373.html