HDU 4436 str2int (广义SAM)
Posted konjak魔芋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4436 str2int (广义SAM)相关的知识,希望对你有一定的参考价值。
str2int
Problem DescriptionIn 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.
InputThere 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.
OutputAn integer between 0 and 2011, inclusive, for each test case.
Sample Input5101123090001234567890
Sample Output202
Source
【分析】
广义SAM,每个点记录一个ans,表示它代表的串的值的和。如果有前缀0,记得不能加进去,每个点标记一下它表示的串又做少个是有前缀0的,记为zr,用父亲的ans和zr更新儿子。
if(now==1&&k==0) t[np].zr=1;
else t[np].zr+=t[now].zr;
t[np].ans=(t[np].ans+t[now].ans*10+(t[now].step-t[t[now].pre].step-t[now].zr)*k);
SAM上有一步是新加一个点,继承某一个点的信息,记得这里要搞一下,把旧点信息减新点信息。
建完机就可以直接统计答案了哈哈哈!!!
具体看代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 #include<set> 9 using namespace std; 10 #define Mod 2012 11 #define Maxn 1000010 12 13 int n; 14 struct node 15 { 16 int pre,step,son[30]; 17 int ans,zr; 18 }t[2*Maxn];int tot; 19 int last; 20 21 char s[Maxn]; 22 23 void upd(int x) 24 { 25 memset(t[x].son,0,sizeof(t[x].son)); 26 t[x].pre=0; 27 t[x].ans=0;t[x].zr=0; 28 } 29 30 void add(int now,int k,int np) 31 { 32 // if(now==1&&k==0) return; 33 if(now==1&&k==0) t[np].zr=1; 34 else /*if(k==0)*/ t[np].zr+=t[now].zr; 35 t[now].son[k]=np; 36 t[np].ans=(t[np].ans+t[now].ans*10+(t[now].step-t[t[now].pre].step-t[now].zr)*k)%Mod; 37 } 38 39 void extend(int k) 40 { 41 int np=++tot;upd(tot); 42 t[np].step=t[last].step+1; 43 int now=last; 44 while(now&&!t[now].son[k]) 45 { 46 add(now,k,np);// t[now].son[k]=np; 47 now=t[now].pre; 48 } 49 if(!now) t[np].pre=1; 50 else 51 { 52 int p=now,q=t[now].son[k]; 53 if(t[p].step+1==t[q].step) t[np].pre=q; 54 else 55 { 56 int nq=++tot;upd(tot); 57 memcpy(t[nq].son,t[q].son,sizeof(t[nq].son)); 58 t[nq].pre=t[q].pre; 59 t[q].pre=t[np].pre=nq; 60 t[nq].step=t[p].step+1; 61 while(now&&t[now].son[k]==q) 62 { 63 // t[now].son[k]=nq; 64 add(now,k,nq); 65 now=t[now].pre; 66 } 67 t[q].ans=(t[q].ans+Mod-t[nq].ans)%Mod; 68 t[q].zr-=t[nq].zr; 69 } 70 } 71 last=np; 72 } 73 74 int main() 75 { 76 while(scanf("%d",&n)!=EOF) 77 { 78 tot=0; 79 t[++tot].step=1;upd(tot); 80 for(int i=1;i<=n;i++) 81 { 82 scanf("%s",s+1); 83 int l=strlen(s+1); 84 last=1; 85 for(int j=1;j<=l;j++) 86 { 87 int k=s[j]-‘0‘; 88 extend(k); 89 } 90 } 91 int ans=0; 92 for(int i=1;i<=tot;i++) ans=(ans+t[i].ans)%Mod; 93 printf("%d\n",ans); 94 } 95 return 0; 96 }
2016-09-21 20:51:06
以上是关于HDU 4436 str2int (广义SAM)的主要内容,如果未能解决你的问题,请参考以下文章