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 ...
随机推荐
- Linux文件保护禁止修改、删除、移动文件等,使用chattr +i保护
不让用户修改.删除文件等,使用 chattr保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[m ...
- Chrome远程调试Android上Chrome的页面
下载最新的google chrome canary 手机chrome开启usb网页调试,如图所示: 在终端运行 ? SHELL adb forward tcp:9222 localabstract:c ...
- easy-ui datagrid
Easy-ui引用 <link href="css/EasyUI/themes/icon.css" rel="stylesheet" type=&q ...
- Vijos P1063 迎春舞会之交谊舞 DP
题目链接:https://vijos.org/p/1063 题意:有n(n <= 100)行,每行有2*i-1个可显字符'#'与'-',组成一个倒三角形,问由'-'组成的最大三角形的'-'的个数 ...
- CAShapeLayer--备用
之前讲过CALayer动画相关知识,再来看看更加复杂的CAShapeLayer相关的动画知识. 普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的boun ...
- firefox浏览器删除插件
打开注册表编辑器([开始菜单]→[运行]或 Win+R快捷键打开运行,输入regedit). 2 点击菜单栏[编辑]→[搜索],或者按 Ctrl + F 快捷键,搜索 MozillaPlugins . ...
- win7系统64位eclipse环境超详细暗黑1.4服务器搭建
原地址:http://bbs.gameres.com/thread_223704.html 今天闲来没事,参照论坛中几篇成功案例,自己也搭建了下服务端,这里详细的记录下我的安装过程,方便大家快速搭建. ...
- 互联网科技今年九个兴奋点:O2O深耕细作,可穿戴设备分水岭
http://new.iheima.com/detail/2014/0204/58374.html i黑马观察到,2014年是O2O的深耕细作年,而线上和线下结合这一互联网化趋势,将会向更多产业扩散, ...
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问 已重装office2 ...
- 让QT编译快一点(增加基础头文件)
姚冬,中老年程序员 进藤光.杨个毛.欧阳修 等人赞同 我是来反对楼上某些答案的.我曾经用MFC写了金山词霸(大约20多万行),又用Qt写了YY语音(大约100多万行),算是对两种框架都比较有经验.纠正 ...