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.
 
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.
 
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
 
Sample Output
3
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 ;
}

最新文章

  1. Atitit.你这些项目不都是模板吗?不是原创&#160;&#160;集成和整合的方式大总结
  2. linux下使用远程图形界面
  3. Ubuntu 12.04 安装MySQL
  4. Unity中各个平台的预编译的运用方式
  5. 断言(ASSERT)的用法
  6. 【风雪之隅】写在PHP7发布之际一些话 2015-12-02
  7. IOS 设备参数
  8. http请求利器: 今天配置出了RESTClient,用MAVEN构建了UI运行包
  9. 自己动手实现getElementsByClassName
  10. jsp静态化之简单介绍
  11. Varnish 4.0
  12. BZOJ1294: [SCOI2009]围豆豆Bean
  13. SDK、JDK、JRE、ADB、AVD到底都是啥?
  14. Java中反射机制详解
  15. pycharm工具配置
  16. get通配符
  17. 使用Disruptor实现生产者和消费者模型
  18. thinkphp 无限极 评论
  19. CAT架构的应用与实践 IT大咖说 - 大咖干货,不再错过
  20. laravel更新某一个或几个字段

热门文章

  1. python学习笔记——列表生成式与生成器
  2. python27读书笔记0.3
  3. js replace in multi-line string
  4. 我的PHP之旅--数组的认识(初级)
  5. XSS之学习误区分析
  6. MySQL db优化
  7. loadrunner_analysis技巧_average 和 90% percent
  8. Linux配置系统
  9. [wikioi]二叉树最大宽度和高度
  10. crtmpserver系列之一:流媒体概述