【题目链接】 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. Git 系列(四):在 Git 中进行版本回退

    在这篇文章中,你将学到如何查看项目中的历史版本,如何进行版本回退,以及如何创建 Git 分支以便你可以大胆尝试而不会出现问题. 在你的 Git 项目的历史中,你的位置就像是摇滚专辑中的一个片段,由一个 ...

  2. Docker容器的数据管理

    Docker容器的数据管理 Docker容器的数据管理 什么是数据卷(Data Volume)? 数据卷是经过特殊设计的目录,可以绕过联合文件系统(UFS),为一个或者多个容器提供访问 数据卷设计的目 ...

  3. PHP中mktime() 函数对于日期运算和验证

    mktime() 函数对于日期运算和验证非常有用.它可以自动校正越界的输入: // 语法:mktime(hour,minute,second,month,day,year) echo(date('Y- ...

  4. 联系InfoSphere Streams和OpenMI时对水利模型联系的设计模式的一些考虑

    从<时序计算通用模型接口 OpenMI开发技术及应用>一书中的第一章的对接口要求描述,我想到InfoSphere streams的流数据处理模式刚好可以满足这种模型/数据之间对接的需求. ...

  5. 走进C标准库(8)——"string.h"中函数的实现相关字符串操作函数

    我的strcat: char *strcat(char *dest,char *src) { char * reval = dest; while(*dest) dest++; while(*src) ...

  6. openstack 开发step-by-step

    Set up your Open Stack There are several ways to deploy openstack, Devstack is easily for developer ...

  7. (function(){}())写法的用途

    全局命名空间污染与 IIFE 总是将代码包裹成一个 IIFE(Immediately-Invoked Function Expression),用以创建独立隔绝的定义域.这一举措可防止全局命名空间被污 ...

  8. JavaSE思维导图(四)

  9. su Authentication failure解决

    su Authentication failure解决 关于Ubuntu桌面系统su root时认证失败的问题 1. Ubuntu 默认没有给root用户设置密码,当我们su root命令时, 提示认 ...

  10. 软件开发常用Linux命令

    解压缩 tar -zxvf xxx.tar.gz 文件显示及查找常用于分析log //显示file中包含aaa的行 cat <file>|grep aaa 查看cpu memory基本信息 ...