标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2082 Accepted Submission(s): 744
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int mod=2012; 6 const int maxn=200010; 7 int cnt,last,n,ans,sum[maxn]; 8 int wv[maxn],sa[maxn],tot[maxn]; 9 int ch[maxn][12],fa[maxn],len[maxn]; 10 struct SAM{ 11 void Init(){ 12 memset(ch,0,sizeof(ch)); 13 memset(fa,0,sizeof(fa)); 14 memset(len,0,sizeof(len)); 15 last=cnt=1; 16 } 17 void Insert(int c){ 18 int p=last,np=last=++cnt;len[np]=len[p]+1; 19 while(p&&!ch[p][c])ch[p][c]=np,p=fa[p]; 20 if(!p)fa[np]=1; 21 else{ 22 int q=ch[p][c]; 23 if(len[p]+1==len[q]) 24 fa[np]=q; 25 else{ 26 int nq=++cnt;len[nq]=len[p]+1; 27 memcpy(ch[nq],ch[q],sizeof(ch[q])); 28 fa[nq]=fa[q];fa[q]=fa[np]=nq; 29 while(p&&ch[p][c]==q) 30 ch[p][c]=nq,p=fa[p]; 31 } 32 } 33 } 34 35 void Extend(char *s){ 36 int l=strlen(s);last=1; 37 for(int i=0,c;i<l;i++){ 38 c=s[i]-‘0‘; 39 if(!ch[last][c]||len[ch[last][c]]!=i+1) 40 Insert(c); 41 else 42 last=ch[last][c]; 43 } 44 } 45 }sam; 46 47 char s[maxn]; 48 49 int main(){ 50 while(scanf("%d",&n)!=EOF){ 51 sam.Init(); 52 for(int i=1;i<=n;i++) 53 scanf("%s",s),sam.Extend(s); 54 55 memset(wv,0,sizeof(wv)); 56 memset(sa,0,sizeof(sa)); 57 for(int i=1;i<=cnt;i++) 58 wv[len[i]]++; 59 for(int i=1;i<=cnt;i++) 60 wv[i]+=wv[i-1]; 61 for(int i=cnt;i>=1;i--) 62 sa[--wv[len[i]]]=i; 63 memset(tot,0,sizeof(tot)); 64 memset(sum,0,sizeof(sum)); 65 tot[1]=1;ans=0; 66 for(int i=0;i<cnt;i++){ 67 int x=sa[i]; 68 for(int j=0;j<=9;j++){ 69 if(x==1&&j==0)continue; 70 (tot[ch[x][j]]+=tot[x])%=mod; 71 (sum[ch[x][j]]+=(sum[x]*10+tot[x]*j))%=mod; 72 } 73 (ans+=sum[x])%=mod; 74 } 75 printf("%d\n",ans); 76 } 77 }
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5588815.html