str2int
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2082 Accepted Submission(s): 744
Problem Description
In this problem, you are given several strings that contain only digits from '0' to '9', inclusive.
An example is shown below.
101
123
The set S of strings is consists of the N strings given in the input file, and all the possible substrings of each one of them.
It's boring to manipulate strings, so you decide to convert strings in S into integers.
You can convert a string that contains only digits into a decimal integer, for example, you can convert "101" into 101, "01" into 1, et al.
If an integer occurs multiple times, you only keep one of them.
For example, in the example shown above, all the integers are 1, 10, 101, 2, 3, 12, 23, 123.
Your task is to calculate the remainder of the sum of all the integers you get divided by 2012.
An example is shown below.
101
123
The set S of strings is consists of the N strings given in the input file, and all the possible substrings of each one of them.
It's boring to manipulate strings, so you decide to convert strings in S into integers.
You can convert a string that contains only digits into a decimal integer, for example, you can convert "101" into 101, "01" into 1, et al.
If an integer occurs multiple times, you only keep one of them.
For example, in the example shown above, all the integers are 1, 10, 101, 2, 3, 12, 23, 123.
Your task is to calculate the remainder of the sum of all the integers you get divided by 2012.
Input
There are no more than 20 test cases.
The test case starts by a line contains an positive integer N.
Next N lines each contains a string consists of one or more digits.
It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings ≤100000.
The input is terminated by EOF.
The test case starts by a line contains an positive integer N.
Next N lines each contains a string consists of one or more digits.
It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings ≤100000.
The input is terminated by EOF.
Output
An integer between 0 and 2011, inclusive, for each test case.
Sample Input
5
101
123
09
000
1234567890
Sample Output
202
这道题用后缀自动机可以很好地解决,这里应用的性质是SAM的每一条转移代表的字串都不相同,而且包含所有子串,TOT表示当前有多少个相同前缀,在后面拼接一个数,只需要将原SUM*10再加上当前贡献TOT*j即可。
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 ",ans); 76 } 77 }