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. Java基础——数组复习

    数组是一个变量,存储相同数据类型的一组数据 声明一个变量就是在内存空间划出一块合适的空间 声明一个数组就是在内存空间划出一串连续的空间 数组长度固定不变,避免数组越界   数组是静态分配内存空间的,所 ...

  3. SpringBoot非官方教程 | 第九篇: springboot整合Redis

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot9-redis/ 本文出自方志朋的博客 这篇文章主 ...

  4. 重置按钮_reset

    function formreset(form){ for(var i=0;i<frmMain.length;i++){ if(frmMain.item(i).type=="text& ...

  5. vue入门:实现图片点击切换

    1.实现功能 2.目录结构 3.代码 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  6. Vue--- 使用vuex使用流程 1.0

    Vuex 1.安装vuex npm install  -save vuex 2. 引入 创建store文件夹目录 创建 vuex     指挥公共目录    store['state','action ...

  7. 动态规划(一)POJ1163

    动态规划算法是比较实用的算法之一,结合实际问题能更好的熟悉这个算法 下面以POJ1163为例子 1. 首先是题目大意 :在给定的一个数字三角形中找到一条自上而下的路径,路径每一步只能左下或者右下,最后 ...

  8. 构建高可靠hadoop集群之5-服务级别授权

    本人翻译自: http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/ServiceLevelAuth.html ...

  9. 数字滤波器的MATLAB与FPGA实现--Altera/Verilog版的pdf版,杜勇等编著的书。

    自己在网上找了很久才找到的资源,花了很大的劲,觉得不易,特地分享给大家.本书讲了使用FPGA的Fir IIR IP核与Matlab配合使用生成滤波器的详细使用方法.贴出地址,http://downlo ...

  10. GIT LFS 使用笔记

    一.背景 由于git上传文件大小受限,所以我们需要使用GIT LFS对大小超过一定上限的大文件进行处理. 二.安装 linux上安装参见 https://askubuntu.com/questions ...