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

“123” ⇒ 123不允许使用 atoi 等其他类似的库函数；一种 naive 版：int str2int(const char* str) { if (str == NULL) ...
• lanchunhui
• 2016年03月26日 17:30
• 1125

hdu 4436 str2int（后缀数组）

str2int Problem Description In this problem, you are given several strings that contain only d...
• a709743744
• 2016年03月01日 09:20
• 404

hdu 4436 str2int 后缀数组

• yrleep
• 2013年11月13日 00:49
• 939

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

http://acm.hdu.edu.cn/showproblem.php?pid=4436 给出n个串，问这些串中所有不同的子串可组成的数字之和模2012的结果是多少？ 将n个串接到一起，中间用分隔...
• wh2124335
• 2013年11月07日 13:46
• 849

实现自己的string2int

• wangshubo1989
• 2016年04月16日 01:39
• 9002

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

• wdcjdtc
• 2014年09月02日 16:17
• 510

str2int

• u012513972
• 2017年12月18日 11:20
• 12754

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

• YxuanwKeith
• 2016年02月05日 13:13
• 17522

HDU 4436 str2int 后缀数组 字符串哈希 前缀和

• u012139398
• 2014年09月12日 15:29
• 579

后缀数组详解

• qq_34731703
• 2016年10月26日 14:50
• 3534

举报原因： 您举报文章：【后缀数组】Str2int 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)