标签:
5 101 123 09 000 1234567890
202
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int mod = 2012; 4 const int maxn = 210001; 5 struct node { 6 int son[11],f,len; 7 void init() { 8 f = -1; 9 len = 0; 10 memset(son,-1,sizeof son); 11 } 12 }; 13 struct SAM { 14 node e[maxn<<1]; 15 int tot,last; 16 int newnode(int len = 0) { 17 e[tot].init(); 18 e[tot].len = len; 19 return tot++; 20 } 21 void init() { 22 tot = last = 0; 23 newnode(0); 24 } 25 void extend(int c) { 26 int p = last,np = newnode(e[p].len + 1); 27 while(p != -1 && e[p].son[c] == -1) { 28 e[p].son[c] = np; 29 p = e[p].f; 30 } 31 if(p == -1) e[np].f = 0; 32 else{ 33 int q = e[p].son[c]; 34 if(e[p].len + 1 == e[q].len) e[np].f = q; 35 else{ 36 int nq = newnode(); 37 e[nq] = e[q]; 38 e[nq].len = e[p].len + 1; 39 e[np].f = e[q].f = nq; 40 while(p != -1 && e[p].son[c] == q){ 41 e[p].son[c] = nq; 42 p = e[p].f; 43 } 44 } 45 } 46 last = np; 47 } 48 }sam; 49 char str[maxn]; 50 int cnt[maxn<<1],c[maxn<<1],sum[maxn<<1],sa[maxn<<1]; 51 int main() { 52 int n,len; 53 while(~scanf("%d",&n)){ 54 sam.init(); 55 for(int i = len = 0; i < n; ++i){ 56 scanf("%s",str + len); 57 len = strlen(str); 58 if(i + 1 < n) str[len++] = ‘0‘ + 10; 59 } 60 for(int i = 0; i < len; ++i) 61 sam.extend(str[i] - ‘0‘); 62 memset(c,0,sizeof c); 63 memset(cnt,0,sizeof cnt); 64 memset(sum,0,sizeof sum); 65 for(int i = 0; i < sam.tot; ++i) c[sam.e[i].len]++; 66 for(int i = 1; i <= len; ++i) c[i] += c[i-1]; 67 for(int i = sam.tot-1; i >= 0; --i) sa[--c[sam.e[i].len]] = i; 68 int ret = 0; 69 cnt[0] = 1; 70 for(int i = 0; i < sam.tot; ++i){ 71 int x = sa[i]; 72 for(int j = 0; j < 10; ++j){ 73 if(x == 0 && j == 0) continue; 74 cnt[sam.e[x].son[j]] += cnt[x]; 75 sum[sam.e[x].son[j]] += sum[x]*10 + cnt[x]*j; 76 cnt[sam.e[x].son[j]] %= mod; 77 sum[sam.e[x].son[j]] %= mod; 78 } 79 ret = (ret + sum[x])%mod; 80 } 81 printf("%d\n",ret); 82 } 83 return 0; 84 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4910888.html