# str2int

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1089    Accepted Submission(s): 360

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.

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.

Output
An integer between 0 and 2011, inclusive, for each test case.

Sample Input
5 101 123 09 000 1234567890

Sample Output
202

Source

Recommend
zhoujiaqi2010

#include <cstdio>
#include <cstring>
#define REP(i,n) for(int i=0;i<(n);i++)
const int maxn = 300000;
const int MOD = 2012;

int sa[maxn],height[maxn],rank[maxn],V[maxn],T[maxn],arrive[maxn],Pow[maxn],P[maxn];

int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void DA(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for (i=0;i<m;i++) ws[i]=0;
for (i=0;i<n;i++) ws[x[i]=r[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for (j=1,p=1;p<n;j*=2,m=p)
{
for (p=0,i=n-j;i<n;i++) y[p++] = i;
for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
for (i=0;i<n;i++) wv[i]=x[y[i]];
for (i=0;i<m;i++) ws[i]=0;
for (i=0;i<n;i++) ws[wv[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void calheight(int *r,int *sa,int n)
{
int i,j,k=0;
for (i=0;i<n;++i) rank[sa[i]] = i;
for (i=0;i<n;height[rank[i++]] = k)
if (rank[i]) for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];++k);
}

char tmp[maxn];
int str[maxn];

int get(int l,int r)
{
if (l > r) return 0;
int ans = (T[r+1]-T[l])%MOD;
ans -= V[l]*(Pow[r-l+1]);
ans %= MOD;
if (ans<0) ans += MOD;
return ans;
}

int main()
{
int n;

Pow[0] = 1; Pow[1] = 10;
for (int i=2;i<200000;++i)
Pow[i] = (Pow[i-1]+1)*10%MOD;

while (scanf("%d",&n)!=EOF)
{
int len = 0;
int val = 0;
REP(i,n)
{
scanf("%s",tmp);
int tl = strlen(tmp);
REP(j,tl)
{
str[len++] = tmp[j]-'0'+1;
val = (val*10+str[len-1]-1)%MOD;
P[len-1] = i;
V[len] = val;
T[len] = (T[len-1]+val)%MOD;
}
str[len++] = 11;
P[len-1] = 11;
V[len] = val;
T[len] = (T[len-1]+val)%MOD;
arrive[i] = len-2;
}
str[len-1] = 0;

DA(str,sa,len,12);
calheight(str,sa,len);

int ans = 0;
REP(i,len)
{
if (str[i]!=1 && str[i]!=11)
if (i+height[rank[i]]<=arrive[P[i]])
{
ans += get(i,arrive[P[i]])-get(i,i+height[rank[i]]-1);
ans %= MOD; if (ans < 0) ans += MOD;
}
}
printf("%d\n",ans);
}

return 0;
}

#### [面试] 算法（一） —— Str2Int

2016-03-26 17:30:46

#### hdu 4436 str2int 后缀数组

2013-11-13 00:49:51

#### hdu 4436 str2int（后缀数组）

2016-03-01 09:20:50

#### 实现自己的string2int

2016-04-16 01:39:27

#### C++学习： int2str，int2str类型转换

2012-08-18 13:10:24

#### [后缀数组+思路] hdu 4436 str2int

2014-09-02 16:17:03

#### hdu 4436 str2int 后缀数组、后缀自动机

2013-11-07 13:46:06

#### 五分钟搞懂后缀数组！后缀数组解析以及应用(附详解代码)

2016-02-05 13:13:36

#### 后缀数组应用——回文子串

2016-01-05 09:02:22

#### 后缀数组详解

2016-10-26 14:50:36