str2int

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 4436
64-bit integer IO format: %I64d      Java class name: Main

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

解题:后缀自动机大法好
 #include <bits/stdc++.h>
using namespace std;
const int mod = ;
const int maxn = ;
struct node {
int son[],f,len;
void init() {
f = -;
len = ;
memset(son,-,sizeof son);
}
};
struct SAM {
node e[maxn<<];
int tot,last;
int newnode(int len = ) {
e[tot].init();
e[tot].len = len;
return tot++;
}
void init() {
tot = last = ;
newnode();
}
void extend(int c) {
int p = last,np = newnode(e[p].len + );
while(p != - && e[p].son[c] == -) {
e[p].son[c] = np;
p = e[p].f;
}
if(p == -) e[np].f = ;
else{
int q = e[p].son[c];
if(e[p].len + == e[q].len) e[np].f = q;
else{
int nq = newnode();
e[nq] = e[q];
e[nq].len = e[p].len + ;
e[np].f = e[q].f = nq;
while(p != - && e[p].son[c] == q){
e[p].son[c] = nq;
p = e[p].f;
}
}
}
last = np;
}
}sam;
char str[maxn];
int cnt[maxn<<],c[maxn<<],sum[maxn<<],sa[maxn<<];
int main() {
int n,len;
while(~scanf("%d",&n)){
sam.init();
for(int i = len = ; i < n; ++i){
scanf("%s",str + len);
len = strlen(str);
if(i + < n) str[len++] = '' + ;
}
for(int i = ; i < len; ++i)
sam.extend(str[i] - '');
memset(c,,sizeof c);
memset(cnt,,sizeof cnt);
memset(sum,,sizeof sum);
for(int i = ; i < sam.tot; ++i) c[sam.e[i].len]++;
for(int i = ; i <= len; ++i) c[i] += c[i-];
for(int i = sam.tot-; i >= ; --i) sa[--c[sam.e[i].len]] = i;
int ret = ;
cnt[] = ;
for(int i = ; i < sam.tot; ++i){
int x = sa[i];
for(int j = ; j < ; ++j){
if(x == && j == ) continue;
cnt[sam.e[x].son[j]] += cnt[x];
sum[sam.e[x].son[j]] += sum[x]* + cnt[x]*j;
cnt[sam.e[x].son[j]] %= mod;
sum[sam.e[x].son[j]] %= mod;
}
ret = (ret + sum[x])%mod;
}
printf("%d\n",ret);
}
return ;
}

最新文章

  1. BestCoder Round 69 Div 2 1001&amp;&amp; 1002 || HDU 5610 &amp;&amp; 5611
  2. [goa]golang微服务框架学习(三)-- 使用swagger-ui展示API
  3. Fragment开发计划
  4. iOS 和 Android 中的Alert
  5. MVC5----用户登陆及验证码
  6. 【socket.io研究】1.官网的一些相关说明,概述
  7. C-01背包问题
  8. CF #April Fools Day Contest 2016 E Out of Controls
  9. webSocket浏览器握手不成功(解决)
  10. WordCount项目
  11. hibernate框架学习笔记3:API详解
  12. DWM1000 测距原理简单分析 之 SS-TWR代码分析2 -- [蓝点无限]
  13. 查看linux是ubuntu还是centos
  14. elasticsearch5安装
  15. python 05集合
  16. Python_函数的镶嵌和作用域链_26
  17. ASP.NET MVC高亮显示当前页面菜单
  18. alsa声卡分析alsa-utils调用过程(一)-tinyplay
  19. mysql 数据操作 多表查询 多表连接查询 全外连接
  20. P3623 [APIO2008]免费道路

热门文章

  1. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
  2. android动画(1)各种动画属性表,简单代码,xml配置
  3. Angularjs中表格的增删改查
  4. 第一章、 CLR的执行模型
  5. JavaScript——数组的indexOf()方法在IE8中的兼容性问题
  6. git 初识
  7. Javascript中setTimeout()以及clearTimeout( )的使用
  8. COGS 2111. [NOIP2015普及]扫雷游戏
  9. codevs 1043 方格取数 2000年NOIP全国联赛提高组
  10. Types of Security Vulnerabilities