HDU 4436 str2int(后缀自动机)
【题目链接】 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(后缀自动机)的更多相关文章
- str2int HDU - 4436 (后缀自动机)
str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...
- 字符串(多串后缀自动机):HDU 4436 str2int
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4436 str2int
str2int Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 4 ...
- HDU 4622 Reincarnation 后缀自动机
模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- HDU 6583 Typewriter(后缀自动机)
Typewrite \[ Time Limit: 1500 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),现在你需要构造出这个字符串,你每次可 ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- Good Article Good sentence HDU - 4416 (后缀自动机)
Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...
- HDU - 6583 Typewriter (后缀自动机+dp)
题目链接 题意:你要打印一段字符串,往尾部添加一个字符需要花费p元,复制一段字符到尾部需要花费q元,求打印完全部字符的最小花费. 一开始想的贪心,后来发现忘了考虑p<q的情况了,还纳闷怎么不对. ...
随机推荐
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 使用fastcgi_cache加速网站
为了提高网站的性能缓存是一把利器,nginx中可以配置fastcig_cache来缓存不需要实时获取的数据实现动静分离,nginx.conf配置如下: http { - fastcgi ...
- js返回值
看下js变量.作用域.内存文档. 1. <script type="text/javascript"> function c(){ return 23; } var a ...
- SQL Server 固定角色
1. 查看固定服务器角色 execute sp_helpsrvrole; 管理: execute master..sp_addsrvrolemember @logingName='neeky' @ro ...
- 贴片方式COB COF COG
英文简称: COB英文全称: Chip On Board中文全称: 通过邦定将IC裸片固定于印刷线路板上 英文简称: COF 英文全称: Chip On FPC 中文全称: 将IC固定于柔性线路板上 ...
- mini KMS Activator v1.3破解激活microsoft Office 2010 RTM
利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...
- ARM Cortex M3(V7-M架构)硬件启动程序 二
解析 STM32 的启动过程 解析STM32的启动过程 当前的嵌入式应用程序开发过程里,并且C语言成为了绝大部分场合的最佳选择.如此一来main函数似乎成为了理所当然的起点——因为C程序往往从main ...
- delphi 实现vip126发邮件
本例是 TSimpleThread , TSimpleList, IdhttpEx 网页模拟(POST)的综合运用. Demo只写了发送,但亦可收取,详见源码. (此源码写于2年前,那时还写得不好,请 ...
- 保存网页为图片——滚动截取IE(WebBrowse)
对IE进行编程一直觉得是相当可怕的事情,里面的接口.函数.事件之多,解释之乱,需要了解的方方面面知识之博,让我仿佛看到了微软就是造物主,因为它已成功制造了这样的混沌,弄就了宇宙的初始状态…… 近来做个 ...
- C++实现Http Post请求
参考资料: http://apps.hi.baidu.com/share/detail/39003388 http://blog.csdn.net/yc0188/article/details/474 ...