Problem Description
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.
 
题目大意:计算n个串的所有子串(重复的算一个)的和,结果求余2012。
思路:把n个字符串用一个奇怪的东东(比如10)连起来,建一个后缀自动机。
由于在后缀自动机上,每一条路径到一个点,都唯一代表着一个字符串,所以重复什么的就没有了。
然后后缀自动机是一个DAG,随便搞搞就能做了。
PS:对后缀自动机熟悉的话,这题还是挺水的,是当时后缀自动机还不普及的缘故么。
 
代码(406MS):
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = + ;
const int MOD = ;
char buf[MAXN];
struct State {
State *fail, *go[];
int val, dp, cnt;
bool mark;
/*
State() :
fail(0), val(0) {
memset(go, 0, sizeof go);
}*/
}*root, *last;
State statePool[MAXN * ], *cur; void init() {
memset(statePool, , sizeof(statePool));
cur = statePool;
root = last = cur++;
} void extend(int w) {
State *p = last, *np = cur++;
np->val = p->val + ;
while (p && !p->go[w])
p->go[w] = np, p = p->fail;
if (!p) np->fail = root;
else {
State*q = p->go[w];
if (p->val + == q->val) np->fail = q;
else {
State *nq = cur++;
memcpy(nq->go, q->go, sizeof q->go);
nq->val = p->val + ;
nq->fail = q->fail;
q->fail = nq;
np->fail = nq;
while (p && p->go[w] == q)
p->go[w] = nq, p = p->fail;
}
}
last = np;
} inline void update_add(int &a, const int &b) {
a = (a + b) % MOD;
} struct Node {
State *p;
bool operator < (const Node &rhs) const {
return p->val < rhs.p->val;
}
} a[MAXN * ]; int main() {
int n;
while(scanf("%d", &n) != EOF) {
init();
for(int i = ; i <= n; ++i) {
scanf("%s", buf);
for(char *pt = buf; *pt; ++pt)
extend(*pt - '');
extend();
}
int m = , ans = ;
for(State *p = statePool; p != cur; ++p) a[m++].p = p;
sort(a, a + m);
root->cnt = ;
for(int i = ; i < m; ++i) {
State *pt = a[i].p;
if(pt == root->go[] || pt->mark) continue;
update_add(ans, pt->dp);
for(int j = ; j < ; ++j) {
if(!pt->go[j]) continue;
update_add(pt->go[j]->dp, * pt->dp + pt->cnt * j);
update_add(pt->go[j]->cnt, pt->cnt);
}
if(pt->go[]) pt->go[]->mark = true;
}
printf("%d\n", ans);
}
}

HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)的更多相关文章

  1. HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )

    Sum of divisors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

  3. HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)

    Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...

  4. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  5. HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)

    Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...

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

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

  7. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang

    感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...

  8. HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

    Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...

  9. HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)

    Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...

随机推荐

  1. 学习MySql和MongoDB笔记

    首先了解下关系型数据库和非关系型数据库 关系型数据库 SQL关系型数据库采用了关系模式来组织数据,即关系模式为二维表格模型. 主要的数据库:SQL Server,Oracle,Mysql,Postgr ...

  2. js中实现页面跳转(返回前一页、后一页)

    一:JS 重载页面,本地刷新,返回上一页 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a ...

  3. 关于Linux环境变量DISPLAY的设置

    问题描述:在个人PC(windows系统)安装了虚拟机,虚拟机中安装了Linux系统,Linux系统中安装了wireshark和firefox这两个程序,网上查阅可以通过设置DISPLAY环境变量指向 ...

  4. kali下添加用户和权限分配

    1.添加用户 useradd -m test #-m的意思是创建用户的主目录 2.为用户test设置密码. passwd test 3.为添加的用户赋予权限(-a 添加 :-G 群组) 如果没有这一步 ...

  5. js判断两个日期是否在几个月之内

    //比较两个时间 time1,time2均为日期类型 //判断两个时间段是否相差 m 个月 function completeDate(time1 , time2 , m) { var diffyea ...

  6. C#中的线程(二)线程同步基础 (读后感)

    参考文章:https://www.cnblogs.com/dingfangbo/p/5769501.html 一.lock 确保只有一个线程访问某个资源或某段代码.通俗的讲就是多个线程操作相同的锁对象 ...

  7. ThinkPHP框架目录的介绍

    library目录 Think目录 mvc

  8. Hadoop(24)-Hadoop优化

    1. MapReduce 跑得慢的原因 优化方法 MapReduce优化方法主要从六个方面考虑:数据输入.Map阶段.Reduce阶段.IO传输.数据倾斜问题和常用的调优参数. 数据输入 Map阶段 ...

  9. django模型的字段查询

    条件运算符 exact: 查判等 list=BookInfo.objects.filter(id__exact=1) 可简写为: list=BookInfo.objects.filter(id=1) ...

  10. Windows和Linux系统下,虚拟环境安装的全面说明和详细步骤

    虚拟环境的创建和使用 用途: ​ 1.在同一台电脑安装同一个包的不同版本 2.记录项目所用的所有的包的版本,方便部署. 如何使用: 1.创建虚拟环境 mkvirtualenv 虚拟环境名 -p pyt ...