字符串(多串后缀自动机):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 ...
随机推荐
- dedecms 5.7文章编辑器附件上传图标不显示
我最近发现在使用dedecms 5.7文章编辑器附件上传图标不显示了,以前是没有问题的,这个更新系统就出来问题了,下面我来给大家分享此问题解决办法. 问题bug:在dedecms 5.7中发现了一 ...
- Eclipse清除SVN密码
1. Eclipse/Subclipse并不会记录用户名及密码, SVN密码是由SVN客户端(JavaHL or SVNKit)负责维持的 2. 可查看Eclipse的Preferences中SVN属 ...
- java可变参数Varargs
http://www.cnblogs.com/shishm/archive/2012/01/31/2332656.html J2SE 1.5提供了“Varargs”机制.借助这一机制,可以定义能和多个 ...
- dnw for linux: Ubuntu下可用,无需编译驱动,mini2440可用
1.安装所需库文件 sudo apt-get install libusb-dev 2.源代码如下 /* dnw2 linux main file. This depends on libusb. * ...
- 【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)
1.制作lib工程,这里我简单制作一个测试类 2.eclipse中 java Project工程引入方法 2.1.新建个java工程,在属性配置中选择 "Java Build Path&qu ...
- HibernateTool的安装和使用(Eclipse中)
http://blog.sina.com.cn/s/blog_919273e20101g1t7.html
- UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)
iOS 8 后 UIAlertView 和 UIActionSheet 都被合并到了 UIAlertController里面. 文档原文: Important: UIAlertView is dep ...
- angular的post提交
用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...
- apache-php安装mysql简单方法
1.启用mysql功能,在php.ini中 extension=php_mysql.dll extension=php_mysqli.dll 2. 修改extension_dir = "ex ...
- SQL 左外连接查询 将右表中的多行变为左表的一列或多列
示例: --行列互转 /**************************************************************************************** ...