HDU 4436 str2int
str2int
This problem will be judged on HDU. Original ID: 4436
64-bit integer IO format: %I64d Java class name: Main
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
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
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的更多相关文章
- HDU 4436 str2int(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4436 [题目大意] 给出一些字符串,由0~9组成,求出所有不同子串的和. [题解] 将所有字符串添 ...
- 字符串(多串后缀自动机):HDU 4436 str2int
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4436 str2int (后缀自动机SAM,多串建立)
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4436 str2int (后缀自动机)
把所有的字符串连接起来,中间用一个未出现的字符分隔开,题意是求这个串的所有子串(中间不含分隔符)形成的数之和. 把这个串加入SAM,对所有子串进行拓扑排序,从前往后统计. 记录到达这个节点的路径个数c ...
- HDU 4436 (后缀自动机)
HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...
- 【HDU 4436】 str2int (广义SAM)
str2int Problem Description In this problem, you are given several strings that contain only digits ...
- str2int HDU - 4436 (后缀自动机)
str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...
- str2int HDU - 4436 后缀自动机求子串信息
题意: 给出 n 个串,求出这 n 个串所有子串代表的数字的和. 题解; 首先可以把这些串构建后缀自动机(sam.last=1就好了), 因为后缀自动机上从 root走到的任意节点都是一个子串,所有可 ...
随机推荐
- servlet生命周期:
Servlet生命周期分为三个阶段: 1,初始化阶段 servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...
- [转]2010 Ruby on Rails 書單 與 練習作業
原帖:http://wp.xdite.net/?p=1754 ========= 學習 Ruby on Rails 最快的途徑無非是直接使用 Rails 撰寫產品.而這個過程中若有 mentor 指導 ...
- 前端之CSS语法及选择器
一.css语法: css由两大部分组成:选择符和声明,声明由属性和属性值两部分组成; 选择符{属性:属性值;属性:属性值;} 注: a) 属性和属性值之间用冒号连接: b)每条声明结束要加分号: 二. ...
- 2019/05/11 JAVA虚拟机原理堆、栈、方法区概念区别
Java堆 堆内存用于存放由new创建的对象和数组.在堆中分配的内存,由java虚拟机自动垃圾回收器来管理.在堆中产生了一个数组或者对象后,还可以在栈中定义一个特殊的变量,这个变量的取值等于数组或者对 ...
- expect下命令不能解析通配符*的问题
曾遇到这样一段代码:(Bash脚本) 1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/expect -f set HOST "192.168.102.1" ...
- zabbix server端配置
# wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.0.6/zabbix-2.0.6.tar. ...
- js获取上周、本周、下周的时间
//获取上周起始时间结束时间.下周起始时间结束时间开始时间和本周起始时间结束时间;(西方) function getTime(n) { var now = new Date(); var year = ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- json-server && axios
json-server && axios vue2.0项目实战(3)使用axios发送请求 https://www.cnblogs.com/zhouyangla/p/6753673.h ...
- Java代理模式精讲之静态代理,动态代理,CGLib代理
代理(Proxy)是一种设计模式,通俗的讲就是通过别人达到自己不可告人的目的(玩笑). 如图: 代理模式的关键点是:代理对象与目标对象.代理对象是对目标对象的扩展,并会调用目标对象 这三个代理模式,就 ...