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. 线段树(区间合并) HDOJ 3308 LCIS

    题目传送门 题意:线段树操作:1. 单点更新 2. 求区间的LCIS(longest consecutive increasing subsequence) 分析:注意是连续的子序列,就是简单的区间合 ...

  2. innobackupex的使用

    优点: 不暂停服务器创建Innodb热备份 为mysql做增量的备份 在mysql服务器之间做在线表迁移 使创建mysql replication更加容易 备份mysql但不增加服务器的负载 安装:x ...

  3. servlet生命周期:

    Servlet生命周期分为三个阶段: 1,初始化阶段  servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...

  4. RabbitMQ八:交换机类型Exchange Types--Topic介绍

    前言 上一章节,我们说了两个类型,本章我们说一下其三:Topic Exchange Topic Exchange  Topic Exchange – 将路由键和某模式进行匹配.此时队列需要绑定要一个模 ...

  5. #pragma使用分析

    #pragma简介 #pragma用于指示编译器完成一些特定的动作 #pragma所定义的很多指示字是编译器特有的 #pragma在不同的编译器间是不可移植的 预处理器将忽略它不认识的#pragma指 ...

  6. 禁用DRM

    10G: alter system set "_gc_policy_time"=0 scope=spfile sid='*'; alter system set "_gc ...

  7. (转)使用Spring配置文件实现AOP

    http://blog.csdn.net/yerenyuan_pku/article/details/52880558 使用Spring配置文件实现AOP 前面我们已经学会了使用Spring的注解方式 ...

  8. 使用python书写的小说爬虫

    1.写了一个简单的网络爬虫 初期1 (后期将会继续完善) #小说的爬取 import requests import random from bs4 import BeautifulSoup base ...

  9. 打开或关闭CD_ROM

    实现效果: 知识运用: API函数 mciSendString //函数用来向媒体控制接口设备发送命令  声明如下 [DllImport("winmm.dll",EntryPoin ...

  10. selective_search_rcnn.m中代码

    im = imresize(im, [NaN im_width]):把图像转换为宽度为im_width,自动计算列数