【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=4436

【题目大意】

  给出一些字符串,由0~9组成,求出所有不同子串的和。

【题解】

  将所有字符串添加拼接符10连接在一起建立自动机,
  从起点开始遍历所有节点,就能计算所有的子串和了。注意转移的时候只转移0到9节点。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N=200005,mod=2012;
char s[N];
int n;
struct sam{
int p,q,np,nq,cnt,last,a[N][11],l[N],f[N];
sam(){cnt=0;last=++cnt;}
void init(){
cnt=0;last=++cnt;
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
memset(x,0,sizeof(x));
memset(sum,0,sizeof(sum));
memset(t,0,sizeof(t));
}
void extend(int c){
p=last;np=last=++cnt;l[np]=l[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}int b[N],x[N];
void build(){
int len=0;
while(n--){
scanf("%s",s);
for(int i=0;s[i];i++)len++,extend(s[i]-'0');
extend(10),len++;
}for(int i=1;i<=cnt;i++)b[l[i]]++;
for(int i=1;i<=len;i++)b[i]+=b[i-1];
for(int i=1;i<=cnt;i++)x[b[l[i]]--]=i;
}int sum[N],t[N];
int solve(){
int ans=0;
sum[1]=0; t[1]=1;
for(int i=1;i<=cnt;i++){
int p=x[i];
for(int j=0;j<10;j++){
if(i==1&&j==0)continue;
if(a[p][j]){
q=a[p][j];
t[q]=(t[p]+t[q])%mod;
sum[q]=(sum[q]+sum[p]*10+t[p]*j)%mod;
}
}ans=(ans+sum[p])%mod;
}return ans;
}
}sam;
int main(){
while(~scanf("%d",&n)){
sam.init();
sam.build();
printf("%d\n",sam.solve());
}return 0;
}

  

HDU 4436 str2int(后缀自动机)的更多相关文章

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

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

  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(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  4. HDU 4436 str2int

    str2int Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 4 ...

  5. HDU 4622 Reincarnation 后缀自动机

    模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...

  6. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  7. HDU 6583 Typewriter(后缀自动机)

    Typewrite \[ Time Limit: 1500 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),现在你需要构造出这个字符串,你每次可 ...

  8. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  9. Good Article Good sentence HDU - 4416 (后缀自动机)

    Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...

  10. HDU - 6583 Typewriter (后缀自动机+dp)

    题目链接 题意:你要打印一段字符串,往尾部添加一个字符需要花费p元,复制一段字符到尾部需要花费q元,求打印完全部字符的最小花费. 一开始想的贪心,后来发现忘了考虑p<q的情况了,还纳闷怎么不对. ...

随机推荐

  1. Storm基础理论

    Storm流式计算基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB" ...

  2. mysql函数操作(4)

    <?php ... $query = "INSERT INTO contactInfo (name, address, phone) VALUES (?, ?, ?)"; $ ...

  3. c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)

    题目如下: 思路分析: 写出完整的程序: /* 问题描述: 有n根棍子,棍子i的长度为ai.想要从中选出3根棍子组成周长尽可能长的三角形.请输 出最大的周长,若无法组成三角形则输出0. */ #inc ...

  4. MySQLdb的安装

    第一步:下载安装介质 https://pypi.python.org/pypi/MySQL-python 注意虽然模块名叫MySQLdb但是MySQL-python指的就是MySQLdb 第二步:安装 ...

  5. SSH Session Recorder

    If you want to record your root ssh session  create a file .bash_profile  . and copy below line by l ...

  6. Cortex-M3学习日志(一)-- GPIO实验

    因为项目所需,所以不得不开始研究M3,我用的是NXP公司的LPC1768这个芯片,它是具有三级流水线的哈佛结构,带独立的本地指令和数据总线以及用于外设的稍微低性能的第三条总线,还包含一个支持随机跳转的 ...

  7. javascript数组顺序-----1冒泡的另一种比较好理解的写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 使用iscroll插件实现下拉刷新功能

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. Android的应用程序的异常处理2

    1.自定义一个类(MaApp)继承Application 2.在清单文件中的Application选项菜单对应的name属性中添加MyApp 3.重写application中的onCreate方法 4 ...

  10. Validform表单验证的完美解决方案,推荐给大家

    http://validform.rjboy.cn/ 功能简介: 可以在input上直接绑定正则,可以自定义datatype,自定义datatype可以是正则,也可以是函数,datatype可以累加或 ...