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
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  

pid=5279" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5279 5278 5277 5276 5275 



题意:
求区间内不同子串的个数

思路:
论文里面有求整个串的不同子串的个数。我们能够引申到这道题
对于整个串,我们的求法是全部子串数减去全部height的值,而height就是lcp
那么对于某个区间,我们仅仅要求出全部包括在这个区间的后缀。然后减去互相之间的lcp就可以
关键是我们要保持这个区间的后缀的字典序

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 2005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank[N],height[N],s[N],a[N];
char str[N],str1[N],str2[N];
#define F(x) ((x)/3+((x)%3==1? 0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包括末尾加入的0
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++) wsf[i]=0;
for(i=0; i<n; i++) wsf[x[i]=r[i]]++;
for(i=1; i<m; i++) wsf[i]+=wsf[i-1];
for(i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i;
p=1;
j=1;
for(; p<n; j*=2,m=p)
{
for(p=0,i=n-j; i<n; i++) y[p++]=i;
for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0; i<n; i++) wv[i]=x[y[i]];
for(i=0; i<m; i++) wsf[i]=0;
for(i=0; i<n; i++) wsf[wv[i]]++;
for(i=1; i<m; i++) wsf[i]+=wsf[i-1];
for(i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i];
t=x;
x=y;
y=t;
x[sa[0]]=0;
for(p=1,i=1; i<n; i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
}
}
void getheight(int *r,int n)//n不保存最后的0
{
int i,j,k=0;
for(i=1; i<=n; i++) rank[sa[i]]=i;
for(i=0; i<n; i++)
{
if(k)
k--;
else
k=0;
j=sa[rank[i]-1];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} int Log[N];
int best[30][N]; void setLog()
{
Log[0] = -1;
for(int i=1; i<N; i++)
{
Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1 ;
}
}
void RMQ(int n) //初始化RMQ
{
for(int i = 1; i <= n ; i ++) best[0][i] = height[i];
for(int i = 1; i <= Log[n] ; i ++)
{
int limit = n - (1<<i) + 1;
for(int j = 1; j <= limit ; j ++)
{
best[i][j] = min(best[i-1][j] , best[i-1][j+(1<<i>>1)]);
}
}
}
int LCP(int a,int b) //询问a,b后缀的最长公共前缀
{
a ++;
int t = Log[b - a + 1];
return min(best[t][a] , best[t][b - (1<<t) + 1]);
}
int t,n,m; int solve(int l,int r,int n)
{
int ans = (r-l+1)*(r-l+2)/2;
int last = -1;
int cnt = r-l+1;
for(int i = 1; i<=n; i++)
{
if(!cnt) break;
if(sa[i]<l || sa[i]>r) continue;
cnt--;
if(last == -1)
{
last = i;
continue;
}
int a = last;
int b = i;
if(a>b) swap(a,b);
int lcp = LCP(a,b);
int la = r-sa[last]+1;//区间内该串的尾部
int lb = r-sa[i]+1;
if(la>=lb && lcp>=lb);//la包括lb了,那么就用la继续往后比較,借此保持字典序,来模拟得到该区间的全部height
else last = i;
ans-=min(lcp,min(la,lb));
}
return ans;
} int main()
{
int i,j,k,len,l,r;
scanf("%d",&t);
setLog();
W(t--)
{
scanf("%s",str);
scanf("%d",&m);
len = strlen(str);
for(i = 0; i<len; i++)
s[i] = str[i]-'a'+1;
s[len] = 0;
getsa(s,sa,len+1,30);
getheight(s,len);
RMQ(len);
while(m--)
{
scanf("%d%d",&l,&r);
printf("%d\n",solve(l-1,r-1,len));
}
}
return 0;
}

HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)的更多相关文章

  1. [hdu4622 Reincarnation]后缀数组

    题意:给一个长度为2000的字符串,10000次询问区间[L,R]内的不同子串的个数 思路:对原串的每个前缀求一边后缀数组,询问[L,R]就变成了询问[L,n]了,即求一个后缀里面出现了多少个不同子串 ...

  2. HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给一个字符串,询问某字串的不同字串的个数. 可以用后缀数组来解决,复杂度O(n).先求出倍 ...

  3. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  4. poj2774 Long Long Message 后缀数组求最长公共子串

    题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...

  5. SPOJ DQUERY 求区间内不同数的个数 主席树

    这题跟HDU3333差不多吧. 离线的做法很简单,不再说了 以前做过. 主席树的做法就比较暴力了.. 什么是主席树呢.. 其实是某种称号. 在该题中的体现是可持久化的线段树. 对于一个数 如果以前没出 ...

  6. CSU-1632 Repeated Substrings[后缀数组求重复出现的子串数目]

    评测地址:https://cn.vjudge.net/problem/CSU-1632 Description 求字符串中所有出现至少2次的子串个数 Input 第一行为一整数T(T<=10)表 ...

  7. poj2774 后缀数组 求最长公共子串

    Reference:IOI2009论文 http://www.cnblogs.com/ziyi--caolu/p/3192731.html #include "stdio.h" # ...

  8. Long Long Message (poj2774 后缀数组求最长公共子串)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 19206   Accepted: 79 ...

  9. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

随机推荐

  1. SpringBoot中打包设置,将配置文件打包在外部

    一.每次用maven的打包工具打包的时候 总是将配置文件一起打包进jar中!配置文件有点小修改就要重新打包很麻烦!!!!为了解决这一麻烦!找 了很多方法,下面的配置已经实现可用 我的项目目录结构如下 ...

  2. tortoiseGit怎么记住密码

    tortoiseGit每次pull和push的时候都要输入git密码很是麻烦,下面是tortoiseGit记住密码的步骤 首先在你的项目界面右键 选择setting,这个步骤知识看一下你的名称和ema ...

  3. [Puppeteer] Get a Page's Load Time with Puppeteer (window.profermence.timing)

    In this lesson we are going to use Google's Puppeteer to gather metrics about a page's load time. We ...

  4. ArcGIS Server 10.2 公布Oracle11g数据源的 Feature Service

    安装好arcgis server 10.2及 Desktop 而且确保 arcgis server manager 能够正常启动执行载入服务 1.Oracle 配置 安装好Oracleserver端程 ...

  5. Android开发之视图动画基础

    Android的animation由四种类型组成 XML中  alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转 ...

  6. ES task管理

    Task Management API The Task Management API is new and should still be considered a beta feature. Th ...

  7. javascript系列-class4.函数

    欢迎加入前端交流群来py: 转载请标明出处!                   在火影的世界中存在忍术,忍术是把强大的能量集中在一起以各种各样不同的形式发射出来.怎样使用各种各样的忍术那?通过结印. ...

  8. HD-ACM算法专攻系列(6)——Big Number

    题目描述: 源码: #include"iostream" #include"cmath" using namespace std; #define PI 3.1 ...

  9. HTTP报文头解析

    HTTP报文头解析 本篇博客我们就来详细的聊一下HTTP协议的常用头部字段,当然我们将其分为请求头和响应头进行阐述.下方是报文头每个字段的格式,首先是头部字段的名称,如Accept,冒号后方紧跟的是该 ...

  10. 准备把平台挪到linux

    在上午准备周末胡老师的课程考核的Ppt时,逐渐我觉得不得不把平台挪到linux了.很多并行的应用不只是在linux上效率更高,而且很多包都在linux上.另外如果不及早挪到Linux上,后面遇到的问题 ...