HDU 4622 Reincarnation(SAM)
2024-09-17 17:17:29
Problem Description
Now you are back,and have a task to do:
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.
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.
Input
The first line contains integer T(1<=T<=5), denote the number of the test cases.
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.
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.
Output
For each test cases,for each query,print the answer in one line.
Sample Input
2
bbaba
5
3 4
2 2
2 5
2 4
1 4
baaba
5
3 3
3 4
1 4
3 5
5 5
bbaba
5
3 4
2 2
2 5
2 4
1 4
baaba
5
3 3
3 4
1 4
3 5
5 5
Sample Output
3
1
7
5
8
1
3
8
5
1
1
7
5
8
1
3
8
5
1
Hint
I won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
Author
WJMZBMR
Source
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 ;
}
最新文章
- Atitit.你这些项目不都是模板吗?不是原创&#160;&#160;集成和整合的方式大总结
- linux下使用远程图形界面
- Ubuntu 12.04 安装MySQL
- Unity中各个平台的预编译的运用方式
- 断言(ASSERT)的用法
- 【风雪之隅】写在PHP7发布之际一些话 2015-12-02
- IOS 设备参数
- http请求利器: 今天配置出了RESTClient,用MAVEN构建了UI运行包
- 自己动手实现getElementsByClassName
- jsp静态化之简单介绍
- Varnish 4.0
- BZOJ1294: [SCOI2009]围豆豆Bean
- SDK、JDK、JRE、ADB、AVD到底都是啥?
- Java中反射机制详解
- pycharm工具配置
- get通配符
- 使用Disruptor实现生产者和消费者模型
- thinkphp 无限极 评论
- CAT架构的应用与实践 IT大咖说 - 大咖干货,不再错过
- laravel更新某一个或几个字段