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的情况了,还纳闷怎么不对. ...
随机推荐
- javascript限制input只允许输入数字
在做数据提交的表单时,经常要对input输入内容的类型进行限制,譬如javascript限制input只允许输入数字,最好的方法当然是使用javascript,因为它不用与服务器交互,大大减轻了服务器 ...
- vb 添加状态栏
1.新建一工程2.添加"部件" ms windows common controls 6.03.将StatusBar控件加至窗体中4.右键点击该控件,选"属性" ...
- 【异常处理】java.lang.NoClassDefFoundError
Exception in thread"main" java.lang.NoClassDefFoundError:org/apache/commons/lang/exception ...
- spring常量值注入
<context:property-placeholder location="classpath:resources/*.properties" /> @Value( ...
- U盘常见故障及检修
一般U盘故障分为软故障和硬故障,其中以软故障最为常见. 软故障主要是指U盘有坏块,从而导致U盘能被计算机识别,但没有盘符出现,或者有盘符出现,但当打开U盘时却提示要进行格式化,而格式化又不能成功.前 ...
- 灵活运用Zend框架
$aAwardMem = $this->dao_raward->getAwardAndMem($where,'award_level asc',false,false,false,'awa ...
- 解析nodejs微信开发-2获取ticket
获取ticket是需要拼接url,url中需要获得的tocken 1.先判断有无tocken:若有则拼接url: 2.请求url: a.处理回调数据,注意时间戳和签名处理方式,此两项并未存到ticke ...
- City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- android上下文
在android中常常会遇到与context有关的内容 浅论一下context : 在语句 AlertDialog.Builder builder = new AlertDialog.Builder( ...
- jQuery的三种$()
参考脚本之家“http://www.jb51.net/article/21660.htm” $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuer ...