标签:
aaaa ababcabb aaaaaa #
2 3 3
Problem : 3518 ( Boring counting ) Judge Status : Accepted RunId : 14564325 Language : C++ Author : lwj1994 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
ac代码
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int s[2002]; char str[2002]; int sa[2002],t1[2002],t2[2002],c[2002]; int Rank[2002],height[2002],ans; void build_sa(int s[],int n,int m) { int i,j,p,*x=t1,*y=t2; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[i]=s[i]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(j=1;j<=n;j<<=1) { p=0; for(i=n-j;i<n;i++) y[p++]=i; for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1; x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++; if(p>=n) break; m=p; } } void getHeight(int s[],int n) { int i,j,k=0; for(i=0;i<=n;i++) Rank[sa[i]]=i; for(i=0;i<n;i++) { if(k) k--; j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } } int judge(int n,int len) { int maxn=sa[0],minn=sa[0],ans=0; int i,j; for(i=1;i<=n;i++) { if(height[i]<len) { if(maxn-minn>=len) ans++; maxn=minn=sa[i]; } else { if(maxn<sa[i]) maxn=sa[i]; if(minn>sa[i]) minn=sa[i]; } } if(maxn-minn>=len) ans++; return ans; } int main() { int n; while(scanf("%s",str)!=EOF) { int i; if(strcmp(str,"#")==0) break; int len=strlen(str); for(i=0;i<len;i++) s[i]=str[i]-'a'+1; s[len]=0; build_sa(s,len+1,30); getHeight(s,len); int l=0,r=len; ans=0; for(i=1;i<=len/2;i++) { ans+=judge(len,i); } printf("%d\n",ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 题目3518 Boring counting(后缀数组,求不重叠重复次数最少为2的子串种类数)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47768427