比赛 |
寒假颓废练习 |
评测结果 |
AAAAAAAATT |
题目名称 |
寒假ing |
最终得分 |
80 |
用户昵称 |
FoolMike |
运行时间 |
3.262 s |
代码语言 |
C++ |
内存使用 |
21.85 MiB |
提交时间 |
2017-01-25 18:45:44 |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=2e5+10;
char s[N];
int n,sa[N],r[N],h[N],Wa[N],Wb[N],Wv[N],Ws[N],t[20],lg[N];
bool cmp(int *r,int x,int y,int l){
return r[x]==r[y]&&r[x+l]==r[y+l];
}
void da(){
int i,m=100,*x=Wa,*y=Wb;
memset(Wa,0,sizeof Wa);
memset(Wb,0,sizeof Wb);
for (i=0;i<=m;i++) Ws[i]=0;
for (i=1;i<=n;i++) Ws[x[i]=s[i]]++;
for (i=1;i<=m;i++) Ws[i]+=Ws[i-1];
for (i=n;i;i--) sa[Ws[x[i]]--]=i;
for (int l=1,p=0;p<n;m=p,l<<=1){
for (i=n-l+1,p=0;i<=n;i++) y[++p]=i;
for (i=1;i<=n;i++)
if (sa[i]>l) y[++p]=sa[i]-l;
for (i=1;i<=n;i++) Wv[i]=x[y[i]];
for (i=0;i<=m;i++) Ws[i]=0;
for (i=1;i<=n;i++) Ws[Wv[i]]++;
for (i=1;i<=m;i++) Ws[i]+=Ws[i-1];
for (i=n;i;i--) sa[Ws[Wv[i]]--]=y[i];
swap(x,y);
for (p=x[sa[1]]=1,i=2;i<=n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],l)?p:++p;
}
}
int dp[N][20];
void geth(){
int i,j,k;
for (i=1;i<=n;i++) r[sa[i]]=i;
for (k=h[r[0]]=0,i=1;i<=n;h[r[i++]]=k)
for (k=max(h[r[i-1]]-1,0),j=sa[r[i]-1];s[i+k]==s[j+k];k++);
for (i=1;i<=n;i++) dp[i][0]=h[i];
for (j=1;j<17;j++)
for (i=1;i+t[j]-1<=n;i++)
dp[i][j]=min(dp[i][j-1],dp[i+t[j-1]][j-1]);
}
int lcp(int x,int y){
x=r[x];y=r[y];
if (x>y) swap(x,y);
int k=lg[y-x];
return min(dp[x+1][k],dp[y-t[k]+1][k]);
}
int P(int x){return n*2+2-x;}
int main()
{
t[0]=1;
for (int i=1;i<17;i++){
t[i]=1<<i;
for (int j=t[i-1];j<t[i];j++) lg[j]=i-1;
}
freopen("broken.in","r",stdin);
freopen("broken.out","w",stdout);
int T;scanf("%d",&T);
while (T--){
scanf("%d%s",&n,s+1);
for (int i=1;i<=n;i++) s[i]-='a'-2,s[P(i)]=s[i];
s[n+1]=1;n=n*2+1;
da();geth();n/=2;
int ans=0;
for (int L=1;L<=n;L++)
for (int i=L+1;i<=n;i+=L){
int x=i-L,y=i,len=lcp(x,y)+lcp(P(x),P(y))-1;
ans=max(ans,len/L+1);
}
printf("%d\n",ans);
}
return 0;
}