str2int
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1436 Accepted Submission(s): 493
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
/*给出n个串,问这些串中所有不同的子串可组成的数字之和模2012的结果是多少?
将n个串接到一起,中间用分隔符隔开,然后求长串的后缀数组,解height数组
做过后缀数组的都知道,我们想知道他们中的不同的子串是可以的,但想想这样其实是个暴力的方法,尝试了下果断是TLE的。
但这可以给我们之后的解题提供思路,先来看看怎么找到所有不同子串:
height数组表示的意思是排在第i位的后缀,和排在第i-1位的后缀的最长公共前缀的长度,
比如:
height[i]=3
第i-1位:[123]45$....
第i位: [123]567$....
然后我们在统计第i位开始的不同子串时,我们是不需要计算1,12,123的,新的子串只有1235,12356,123567。
所以第i位开始可构成的新的子串就是从原串的sa[i]+height[i]开始,一直到遇到$为止的那些串,
我们把他们加到一起就可以得到答案了。
但是,简单想想,这些不同的子串太多了,这样必然是TLE。我们要加快计算的过程!
我们计len[i]表示从第i个字符往后,到遇到第一个$时的长度;
我们计sum[i]表示,以第i个字符开始的,直到遇到第一个$的所有子串的和;如123$....,sum[0]=1+12+123=134
这时在计算第i位的时候,sum[i]=1+12+123+1235+12356+123567,sum[i+height[i]]=5+56+567
我们希望只把1235,12356,123567加进去,实际上就是(123*10+5)+(123*100+56)+(123*1000+567)=5+56+567+123*1110
我们可以记录当前公共部分的值num[i]=123,然后将其乘上1110(len[sa[i]+height[i]]个1)再加上sum[i+height[i]]就行了*/
#include<stdio.h>
#include<iostream>
#define mod 2012
#define N 200005
using namespace std;
int t1[N],t2[N],c[N],s[N],sa[N],rank[N],height[N],len[N],sum[N],f[N],num[N];
char str[N];
void build_sa(int *s,int n,int m)
{
int *x=t1,*y=t2,i,k;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=s[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(k=1;k<=n;k<<=1)
{
int p=0;
for(i=n-k;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[y[i]]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[0]]=0; p=1;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]? p-1: p++;
if(p>=n)
break;
m=p;
}
}
void getheight(int n )
{
int i,j,k=0;
for(i=0;i<=n;i++)
rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k) k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
int main()
{
int t,b,n,a,ans,i,j;
f[0]=0; b=1;
for(i=1;i<=N;i++)
{
b=(b*10)%mod;
f[i]=(f[i-1]+b)%mod;
}
while(scanf("%d",&t)!=EOF)
{
n=0;
for(i=1;i<=t;i++)
{
scanf("%s",str);
for(j=0;str[j]!='\0';j++)
s[n++]=str[j];
s[n++]='9'+i;
}
s[n]=0;
build_sa(s,n+1,10100);
getheight(n);
for(i=n;i>=0;i--)
{
a=s[i]-'0';
if(s[i]==0||s[i]>'9')
sum[i]=0,len[i]=0;
else
{
len[i]=len[i+1]+1;
sum[i]=(sum[i+1]+a*(f[len[i+1]])+a)%mod;
}
}
ans=0;
num[0]=0;
int top=0;
for(int i=1;i<=n;i++)
{
if(s[sa[i]]=='0'||s[sa[i]]>'9')
{
top=0;
continue;
}
for(int j=top+1;j<=height[i];j++)
{//根据由height数组来不同分组间的关系,构造num数组,避免多次算重复的前缀,节省时间。
num[j]=(num[j-1]*10+s[sa[i]+j-1]-'0')%mod;
}
top=height[i];
ans=(ans+sum[sa[i]+height[i]]+num[top]*f[len[sa[i]+height[i]]])%mod;
}
printf("%d\n",ans);
}
return 0;
}