【题目链接】 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. Linux学习之echo命令

    语法: # echo [Options] [String] 方括号中的项目是可选的.字符串可以定义为字符的有限序列(如字母,数字,符号,标点符号). 当echo命令不带任何选项或字符串使用时,它会在显 ...

  2. Linux中的那些英文缩写和她的含义们

    系统 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Print working directory 打印工作路径. su:Swith user 切换用户,切换到roo ...

  3. linux修改文本模式下的分辨率(CentOS6.4)

    root登录 vi /boot/grub/menu.lst 看到如下界面: 红框全出位置为分辨率设置,设置参数如下: 保存 shutdown -r now

  4. GMTED2010 高程数据下载

    http://topotools.cr.usgs.gov/GMTED_viewer/viewer.htm

  5. android中onStartActivityForResult无返回值问题

    在activity间跳转传递参数,常见方法是通过onStartActivityForResult来做.不过今天使用 onStartActivityForResult的时候已经在上一个activity调 ...

  6. Servlet的运行方式

    通常我们运行servlet需要在web.xml配置文件中,注册我们写好的servlet以及其对应的访问路径. 在学习web开发中,有一种不需要配置便可以直接对servlet进行配置的方式,在web.x ...

  7. Ubuntu创建快捷方式

    正常安装putty不成功:sudo apt-get install putty 总是报网络方面的错,我只好从官网下载源码自己编译.还是很容易的,找到unix目录,然后执行:make -f Makefi ...

  8. 数据结构中La表的数据合并到Lb表中

    实验描述:La表中的数据为(3,5,8,11)  Lb 表中的数据为(2,6,8,9,11,15,20) 将La表中的数据而不存在Lb表的数据插入到Lb表中,从而实现并集操作. 出现的问题:最后实现的 ...

  9. 推荐两个不错的CAD二次开发(.Net)手册

    推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...

  10. 利用Telnet来模拟Http请求 有GET和POST两种

    利用Telnet来模拟Http请求---访问百度.       1.打开"运行"->cmd进入命令环境:       2.输入"telnet www.baidu.c ...