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. 如何改变CSV文件的编码

    通常我.csv文件的编码都不是我们想要的,比如我要把他保存为Utf-8格式的,好让我可以导入数据库,不乱码 工具/原料电脑,.csv文件方法/步骤1首先,将.csv文件保存一下.然后鼠标右击打开方式记 ...

  2. Linux遗忘命令

    1.查找文件的安装目录,拿nginx来说 find /|grep nginx.conf 2.   a.查询某个端口是否被占用,如8080端口 netstat –apn | grep 8080 b.查看 ...

  3. Ionic之增加样式会自动换行解决方案

    设置样式的时候,引用自身的样式,能正常显示,但是引用自定义样式显示的时候,竟然或自动换行,好尴尬. 原本代码: $('.codeSuccess').css({'display':'block'}); ...

  4. [转]EntityFramework之领域驱动设计实践

    本文转自:http://www.cnblogs.com/daxnet/archive/2010/11/02/1867392.html Entity Framework之领域驱动设计实践 EntityF ...

  5. Map集合的实现类

    Map的继承关系: Map接口的常用实现类: 1.HashMap.Hashtable(t是小写) HashMap不是线程安全的,key.value的值都可以是null. Hashtable是线程安全的 ...

  6. Android学习笔记(十七) BroadcastReceiver

    1.接收广播 创建一个类,继承BroadcastReceiver,复写其中的onReceive()方法 在AndroidManifest.xml文件中注册该BroadcastReceiver 设置完成 ...

  7. spring @value 为什么没有获取到值

    1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired  自动注入的bean 不要用new 出来的bean ...

  8. 2019_京东JAVA实习生招聘机试第一题

    题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...

  9. 网页添加tittle前的图标logo

    在head标签中 <link rel="icon" href="~/favicon.ico" type="image/x-icon" ...

  10. python django项目断点调试