字符串(多串后缀自动机):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 ...
随机推荐
- oracle 字符串切割成结果集方法
oracle字符串切割几种方式 方法一: SELECT COLUMN_VALUE FROM TABLE(SYS.ODCIVARCHAR2LIST('1','2','3','4','5')); 方法二: ...
- oracle 异常管理
命名的系统异常 产生原因 access_into_null 未定义对象 CASE_NOT_FOUND CASE 中若未包含相应的 WHEN ,并且没有设置 ELSE ...
- css3购物网站商品文字提示实例
css3购物网站商品文字提示实例先来看效果图:<ignore_js_op> 当鼠标划过图片时,有着泰迪熊黑色长方形的背景就会出现.来看HTML5+CSS3代码: <!DOCTYPE ...
- DataTbale取值
有一个DataTable数据 //创建DataTable对象 DataTable dt = new DataTable("Table_AX"); //为DataTable创建列 / ...
- selenium+eclipse+python环境
1.下载并安装jdk,配置环境变量: 2.下载并安装python,配置path系统环境变量:D:\Program Files\python34: 3.安装selenium,在安装好的python路径D ...
- MySQL性能测试工具之mysqlslap使用详解
mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msq ...
- IOC之Unity
项目最近用到,查找到比较好的资料,记录哈: Unity 3(一):简介与示例: Unity 3(二):Unity在AOP方面的应用
- 【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别
原文: http://blog.csdn.net/ganlijianstyle/article/details/7924446 1.在 @interface :NSObject{} 的括号中,当然N ...
- hdoj 1087 (DP)
代码: #include<iostream> #include<cmath> using namespace std; int a[1005], dp[1005]; ...
- 寻找链表中倒数第K个结点的位置
输入一个链表,输出该链表中倒数第K个结点. struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* FindKthToTail( ...