str2int
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2542 Accepted Submission(s): 942
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
Source
给你一组串,求这些串的所有子串组成的所有不同数字的和对2012取余的结果。
把所有串连起来建立后缀自动机,拓扑排序,找出所有不同子串对答案的贡献。从根开始向下dp即可,记录每个点的答案和sum[i]和不同的子串个数dp[i]。
要注意,前导0不计入答案,需要跳过。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=120005,maxk=11,inf=0x3f3f3f3f,mod=2012;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn];
class SAM {
public:
void init() {
num=last=0;
a[0].len=0;a[0].fa=-1;
for (int i=0;i<maxk;i++) a[0].son[i]=-1;
}
void update (int c) {
int now=++num,p;
a[now].len=a[last].len+1;
maxlen=max(maxlen,a[now].len);
memset(a[now].son,-1,sizeof(a[now].son));
for (p=last;p!=-1&&a[p].son[c]==-1;p=a[p].fa)
a[p].son[c]=now;
if (p==-1) a[now].fa=0; else {
int q=a[p].son[c];
if (a[p].len+1==a[q].len) {
a[now].fa=q;
} else {
int ne=++num;
a[ne].len=a[p].len+1;
maxlen=max(maxlen,a[ne].len);
memcpy(a[ne].son,a[q].son,sizeof(a[q].son));
a[ne].fa=a[q].fa;
for (;p!=-1&&a[p].son[c]==q;p=a[p].fa)
a[p].son[c]=ne;
a[q].fa=a[now].fa=ne;
}
}
last=now;
}
int getfa(int n) {
return a[n].fa;
}
int getlen(int n) {
return a[n].len;
}
int getmaxlen() {
return maxlen;
}
int getnum() {
return num;
}
int getson(int n,int c) {
return a[n].son[c];
}
private:
int num,last,maxlen;
struct node{
int len,fa;
int son[maxk];
} a[maxn*2];
};
SAM sa;
int w[maxn*2],r[maxn*2],dp[maxn*2],sum[maxn*2];
int topsort() {
int m=sa.getnum(),i,j,ans=0;
mem0(w);
int len=sa.getmaxlen();
for(i=0;i<=m;i++) w[sa.getlen(i)]++;
for(i=0;i<=len;i++) w[i]+=w[i-1];
for(i=m;i>=0;i--) r[--w[sa.getlen(i)]]=i;
int now=0;
mem0(dp);mem0(sum);
dp[0]=1;
for (i=0;i<=m;i++) {
for (j=0;j<10;j++) {
if (i==0&&j==0) continue;
int to=sa.getson(r[i],j);
if (to!=-1) {
dp[to]+=dp[r[i]];
dp[to]%=mod;
sum[to]+=sum[r[i]]*10+j*dp[r[i]];
sum[to]%=mod;
}
}
ans+=sum[r[i]];
ans%=mod;
}
return ans;
}
int main() {
int n;
while (scanf("%d",&n)!=EOF) {
sa.init();
int i,j;
for (i=1;i<=n;i++) {
scanf("%s",s);
int len=strlen(s);
for (j=0;j<len;j++)
sa.update(s[j]-'0');
sa.update(10);
}
int ans=topsort();
printf("%d\n",ans);
}
return 0;
}