HDU 4622 Reincarnation(SAM)
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
For each test cases,the first line contains a string s(1 <= length of s <= 2000).
Denote the length of s by n.
The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.
bbaba
5
3 4
2 2
2 5
2 4
1 4
baaba
5
3 3
3 4
1 4
3 5
5 5
1
7
5
8
1
3
8
5
1
I won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
ps:代码自带大常数=-=
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int N = *1e4+; struct QN {
int l,r,num;
bool operator < (const QN& rhs) const {
return l<rhs.l || (l==rhs.l&&r<rhs.r);
}
}que[N];
char s[N];
int n,sz,last;
int fa[N],ch[N][],l[N],ans[N]; void clear() {
sz=; last=++sz;
memset(ch,,sizeof(ch));
memset(l,,sizeof(l));
}
void add(int x) {
int c=s[x]-'a';
int p=last,np=++sz; last=np;
l[np]=l[p]+;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=;
else {
int q=ch[p][c];
if(l[p]+==l[q]) fa[np]=q;
else {
int nq=++sz; l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
for(;q==ch[p][c];p=fa[p]) ch[p][c]=nq;
}
}
}
int read() {
char c=getchar(); int x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*+c-'',c=getchar();
return x;
}
int main() {
int T,i,j; T=read();
while(T--) {
scanf("%s",s); n=read();
for(i=;i<=n;i++) {
que[i].l=read(),que[i].r=read();
que[i].num=i;
}
sort(que+,que+n+);
memset(ans,,sizeof(ans));
j=que[].l;
for(i=;i<=n;i++) {
if(i==&&que[i].l==que[i-].l)
for(j;j<=que[i].r;j++) add(j-);
else {
clear();
for(j=que[i].l;j<=que[i].r;j++) add(j-);
}
for(j=;j<=sz;j++)
ans[que[i].num]+=l[j]-l[fa[j]];
}
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
}
return ;
}
HDU 4622 Reincarnation(SAM)的更多相关文章
- hdu 4622 Reincarnation(后缀数组)
		
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
 - HDU 4622 Reincarnation(后缀自动机)
		
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
 - HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
		
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
 - Hdu 4622 Reincarnation(后缀自动机)
		
/* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...
 - 后缀自动机(SAM)
		
*在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...
 - 后缀自己主动机(SAM)学习指南
		
*在学习后缀自己主动机之前须要熟练掌握WA自己主动机.RE自己主动机与TLE自己主动机* 什么是后缀自己主动机 后缀自己主动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂 ...
 - HDU 4622 Reincarnation Hash解法详解
		
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
 - HDU 5934 Bomb(炸弹)
		
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
 - HDU 5734 Acperience(返虚入浑)
		
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
 
随机推荐
- [数据库连接字符串] Access 连接字符串
			
[数据库连接字符串] Access 连接字符串 //ODBC 标准安全策略 Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb ...
 - node.js&mongodb&express 搭建个人博客系统
			
源码参见于 https://github.com/njaulj/iliujun
 - codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre
			
C. Lieges of Legendre 题意:给n,m表示有n个为2的倍数,m个为3的倍数:问这n+m个数不重复时的最大值 最小为多少? 数据:(0 ≤ n, m ≤ 1 000 000, n + ...
 - UIViewCotroller 的生命周期函数
			
Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...
 - sjtu1364 countcountcount
			
Description 我有一个元素个数为\(n\)的整数数组\(a\)和\(Q\)个问题,每个问题有\(x,y\)两个参数,我要数有多少个整数\(K\)满足\(K\)在\(a[x]-a[y]\)中出 ...
 - *[topcoder]AstronomicalRecords
			
http://community.topcoder.com/stat?c=problem_statement&pm=12804&rd=15706 首先A和B的长度都不一定一样,里面的元 ...
 - 使用Timer执行定时任务
			
一.Timer概述 在Java开发中,会碰到一些需要定时或者延时执行某些任务的需求,这时,我们可以使用Java中的Timer类实现. 二.Timer介绍 Timer是一个定时器类,通过该类可以为指定的 ...
 - 【HDOJ】1262 寻找素数对
			
典型的二分决策树.而且本身两数和是偶数. #include <stdio.h> #include <string.h> #define MAXNUM 10001 int isP ...
 - Linux 性能监测工具总结
			
前言: Linux系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工具来判断究竟是哪一部分(内存.CPU.硬盘……)出了问题.在Linux系统中,所有的运行参数保存在虚拟目录/ ...
 - poj3373Changing Digits(dp)
			
链接 dfs倒着搜 返回的路径不能满足相同的数最多 借鉴了下别人的代码.. 先dp出来 再倒着标记一下 然后正回来一定可以满足了 dp保存的是最小的不相同数 #include <iostream ...