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 ;
}

HDU 4436 str2int的更多相关文章

  1. HDU 4436 str2int(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4436 [题目大意] 给出一些字符串,由0~9组成,求出所有不同子串的和. [题解] 将所有字符串添 ...

  2. 字符串(多串后缀自动机):HDU 4436 str2int

    str2int Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  3. HDU 4436 str2int (后缀自动机SAM,多串建立)

    str2int Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  4. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  5. HDU 4436 str2int (后缀自动机)

    把所有的字符串连接起来,中间用一个未出现的字符分隔开,题意是求这个串的所有子串(中间不含分隔符)形成的数之和. 把这个串加入SAM,对所有子串进行拓扑排序,从前往后统计. 记录到达这个节点的路径个数c ...

  6. HDU 4436 (后缀自动机)

    HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...

  7. 【HDU 4436】 str2int (广义SAM)

    str2int Problem Description In this problem, you are given several strings that contain only digits ...

  8. str2int HDU - 4436 (后缀自动机)

    str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...

  9. str2int HDU - 4436 后缀自动机求子串信息

    题意: 给出 n 个串,求出这 n 个串所有子串代表的数字的和. 题解; 首先可以把这些串构建后缀自动机(sam.last=1就好了), 因为后缀自动机上从 root走到的任意节点都是一个子串,所有可 ...

随机推荐

  1. JavaScript入门2

    5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...

  2. 通过表单展示不一样的页面(input对象)

    表单中包含不一样的样式,不同功能的提交数据的方式.在许多页面中,浏览者不经意间已经不断在使用表单的功能,如留言,设置自己的密码或者是复选框,下拉列表等. input对象下的多种表单表现形式: 通常在页 ...

  3. Hadoop工作流--JobControl(五)

    不多说,直接上干货! 这只是部分,做个引子. 未完,待续!

  4. Suricata的总体架构

    Suricata的总体架构  报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...

  5. java学习第二章

  6. [转]如何使用MFC和类型库创建自动化项目

    本文转自:http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html 摘要 本文详细介绍了如何自动化像Microsoft Offi ...

  7. AJPFX浅谈Java 性能优化之字符串过滤实战

    ★一个简单的需求 首先描述一下需求:给定一个 String 对象,过滤掉除了数字(字符'0'到'9')以外的其它字符.要求时间开销尽可能小.过滤函数的原型如下: String filter(Strin ...

  8. redis安装(windows)

    软件环境:由于redis只有linux版,而windows版是微软自己做的,最新版只到3.2.100(linux的版本已经到5.x) 1.1.1.  redis安装 首先去官网下载安装包 得到的安装文 ...

  9. 关于 propertychange 兼容性问题

    on 事件 $('body').on('property input','.class',function(){ alert(123); });

  10. js运行机制(线程)

    浏览器线程 js运作在浏览器中,是单线程的,即js代码始终在一个线程上执行,这个线程称为js引擎线程. 浏览器是多线程的,除了js引擎线程,它还有:  UI渲染线程 浏览器事件触发线程 http请求线 ...