字符串(多串后缀自动机):HDU 4436 str2int
str2int
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2082    Accepted Submission(s): 744
An example is shown below.
101
123
The set S of strings is consists of the N strings given in the input file, and all the possible substrings of each one of them.
It's boring to manipulate strings, so you decide to convert strings in S into integers.
You
can convert a string that contains only digits into a decimal integer,
for example, you can convert "101" into 101, "01" into 1, et al.
If an integer occurs multiple times, you only keep one of them.
For example, in the example shown above, all the integers are 1, 10, 101, 2, 3, 12, 23, 123.
Your task is to calculate the remainder of the sum of all the integers you get divided by 2012.
The test case starts by a line contains an positive integer N.
Next N lines each contains a string consists of one or more digits.
It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings ≤100000.
The input is terminated by EOF.
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int mod=;
const int maxn=;
int cnt,last,n,ans,sum[maxn];
int wv[maxn],sa[maxn],tot[maxn];
int ch[maxn][],fa[maxn],len[maxn];
struct SAM{
void Init(){
memset(ch,,sizeof(ch));
memset(fa,,sizeof(fa));
memset(len,,sizeof(len));
last=cnt=;
}
void Insert(int c){
int p=last,np=last=++cnt;len[np]=len[p]+;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else{
int q=ch[p][c];
if(len[p]+==len[q])
fa[np]=q;
else{
int nq=++cnt;len[nq]=len[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
while(p&&ch[p][c]==q)
ch[p][c]=nq,p=fa[p];
}
}
} void Extend(char *s){
int l=strlen(s);last=;
for(int i=,c;i<l;i++){
c=s[i]-'';
if(!ch[last][c]||len[ch[last][c]]!=i+)
Insert(c);
else
last=ch[last][c];
}
}
}sam; char s[maxn]; int main(){
while(scanf("%d",&n)!=EOF){
sam.Init();
for(int i=;i<=n;i++)
scanf("%s",s),sam.Extend(s); memset(wv,,sizeof(wv));
memset(sa,,sizeof(sa));
for(int i=;i<=cnt;i++)
wv[len[i]]++;
for(int i=;i<=cnt;i++)
wv[i]+=wv[i-];
for(int i=cnt;i>=;i--)
sa[--wv[len[i]]]=i;
memset(tot,,sizeof(tot));
memset(sum,,sizeof(sum));
tot[]=;ans=;
for(int i=;i<cnt;i++){
int x=sa[i];
for(int j=;j<=;j++){
if(x==&&j==)continue;
(tot[ch[x][j]]+=tot[x])%=mod;
(sum[ch[x][j]]+=(sum[x]*+tot[x]*j))%=mod;
}
(ans+=sum[x])%=mod;
}
printf("%d\n",ans);
}
}
字符串(多串后缀自动机):HDU 4436 str2int的更多相关文章
- 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 ...
 - BZOJ 3473: 字符串 [广义后缀自动机]
		
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 354 Solved: 160[Submit][Status][Discuss] ...
 - [十二省联考2019]字符串问题——后缀自动机+parent树优化建图+拓扑序DP+倍增
		
题目链接: [十二省联考2019]字符串问题 首先考虑最暴力的做法就是对于每个$B$串存一下它是哪些$A$串的前缀,然后按每组支配关系连边,做一遍拓扑序DP即可. 但即使忽略判断前缀的时间,光是连边的 ...
 - 【BZOJ-1396&2865】识别子串&字符串识别     后缀自动机/后缀树组 + 线段树
		
1396: 识别子串 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 312 Solved: 193[Submit][Status][Discuss] ...
 - 51nod1469 淋漓字符串(后缀自动机)
		
题目大意: 首先,我们来定义一下淋漓尽致子串. 1.令原串为S. 2.设子串的长度为len,在原串S中出现的次数为k,令其出现的位置为p1, p2, ....pk(即这个子串在原串中[pi,pi + ...
 - 【bzoj3277/bzoj3473】串/字符串  广义后缀自动机
		
题目描述 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). 输入 第一行两个整数n,k.接下来n行每行一个 ...
 - 【BZOJ4556】[TJOI2016&HEOI2016] 字符串(后缀自动机+线段树合并+二分)
		
点此看题面 大致题意: 给你一个字符串\(s\),每次问你一个子串\(s[a..b]\)的所有子串和\(s[c..d]\)的最长公共前缀. 二分 首先我们可以发现一个简单性质,即要求最长公共前缀,则我 ...
 - 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
		
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
 
随机推荐
- nginx php7 配置  备用
			
yum install epel-* -y yum install -y wget unzip gcc gcc-c++ make zlib zlib-devel pcre pcre-devel lib ...
 - 每次打开VS2010都会报“ devenv.exe -Assert Failure”这个错误
			
把.net framework4.5中文包卸载掉,, 如果还不行就把.net framework4.5也卸载掉,,然后到微软官网下载net framework4.5安装包安装,安装完后把中文包卸载掉就 ...
 - 把Excel数据导入到数据库
			
引入命名空间 using System.IO; using System.Data; using System.Data.OleDb; 引入命名空间 首先要把Excel上传到服务器 //上传Excel ...
 - onContextItemSelected 用法
			
http://blog.csdn.net/kavensu/article/details/8045041 onCreateOptionsMenu :此方法为创建菜单方法,这个菜单就是你在点击手机men ...
 - SDK Manager.exe 无法启动,一闪而过的解决办法
			
删掉 C:\Windows\system32\ 下的 java.exe.javaw.exe.javaws.exe 即可解决.(转载)
 - Datatables+Bootstrap
			
http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...
 - awk中split函数的用法
			
time='12:34:56' echo $time | awk '{split($0,a,":" ); print a[1]}' 12 echo $time | awk '{sp ...
 - Relative与Absolute组合使用
			
小伙伴们学习了绝对定位的方法:使用position:absolute可以实现被设置元素相对于浏览器(body)设置定位以后, 大家有没有想过可不可以相对于其它元素进行定位呢?答案是肯定的,当然可以.使 ...
 - PV、UV、IP的区别
			
网站推广需要一个网站访问统计工具,常用的统计工具有百度统计.51la.量子恒道统计等.网站访问量常用的指标为PV.UV.IP.那么什么是PV.UV和IP,PV.UV.IP的区别是什么? --首先来看看 ...
 - login-登录
			
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...