字符串(多串后缀自动机):HDU 4436 str2int
2016-06-15 20:25
302 查看
str2int
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2082 Accepted Submission(s): 744
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
An integer between 0 and 2011, inclusive, for each test case.
[align=left]Sample Input[/align]
5
101
123
09
000
1234567890
[align=left] [/align]
[align=left]Sample Output[/align]
202
这道题用后缀自动机可以很好地解决,这里应用的性质是SAM的每一条转移代表的字串都不相同,而且包含所有子串,TOT表示当前有多少个相同前缀,在后面拼接一个数,只需要将原SUM*10再加上当前贡献TOT*j即可。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int mod=2012; const int maxn=200010; int cnt,last,n,ans,sum[maxn]; int wv[maxn],sa[maxn],tot[maxn]; int ch[maxn][12],fa[maxn],len[maxn]; struct SAM{ void Init(){ memset(ch,0,sizeof(ch)); memset(fa,0,sizeof(fa)); memset(len,0,sizeof(len)); last=cnt=1; } void Insert(int c){ int p=last,np=last=++cnt;len[np]=len[p]+1; while(p&&!ch[p][c])ch[p][c]=np,p=fa[p]; if(!p)fa[np]=1; else{ int q=ch[p][c]; if(len[p]+1==len[q]) fa[np]=q; else{ int nq=++cnt;len[nq]=len[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[q])); fa[nq]=fa[q];fa[q]=fa[np]=nq; while(p&&ch[p][c]==q) ch[p][c]=nq,p=fa[p]; } } } void Extend(char *s){ int l=strlen(s);last=1; for(int i=0,c;i<l;i++){ c=s[i]-'0'; if(!ch[last][c]||len[ch[last][c]]!=i+1) Insert(c); else last=ch[last][c]; } } }sam; char s[maxn]; int main(){ while(scanf("%d",&n)!=EOF){ sam.Init(); for(int i=1;i<=n;i++) scanf("%s",s),sam.Extend(s); memset(wv,0,sizeof(wv)); memset(sa,0,sizeof(sa)); for(int i=1;i<=cnt;i++) wv[len[i]]++; for(int i=1;i<=cnt;i++) wv[i]+=wv[i-1]; for(int i=cnt;i>=1;i--) sa[--wv[len[i]]]=i; memset(tot,0,sizeof(tot)); memset(sum,0,sizeof(sum)); tot[1]=1;ans=0; for(int i=0;i<cnt;i++){ int x=sa[i]; for(int j=0;j<=9;j++){ if(x==1&&j==0)continue; (tot[ch[x][j]]+=tot[x])%=mod; (sum[ch[x][j]]+=(sum[x]*10+tot[x]*j))%=mod; } (ans+=sum[x])%=mod; } printf("%d\n",ans); } }
相关文章推荐
- Java反射学习总结(1)——Class类的使用
- 论文:Deep Face Recognition 概括
- Gradle 多渠道打包实践
- 使用cppcheck检测代码警告、错误
- 第16周阅读程序4(2)
- spring MVC配置详解
- 克鲁斯卡尔
- Meet python: little notes 4 - high-level characteristics
- 【Apache Shiro】学习笔记——Authentication基础
- 短信验证
- 漫谈deepcopy(深拷贝)和shallowcopy(浅拷贝)
- 物理层及数据通信基础
- [Android Studio] Android studio 多渠道打包(超简洁版)
- cocos2d-x 3.x学习之RotateAction
- httpUtils网络请求
- 北京大学信息科学研究生上级考试第一题:2703:骑车与走路
- leetcode 287. Find the Duplicate Number
- 实验二
- Gray Code
- hdu 1272 小希的迷宫(并查集)