HDU4622

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

题意

给出一个字符串SSS,∣S∣≤2000|S|\leq 2000∣S∣≤2000,给出QQQ组LLL和RRR,Q≤10000Q\leq 10000Q≤10000,求SSS的子串S[L...R]S[L...R]S[L...R],有多少不同的子串。

题解

对于SSS的所有∣S∣|S|∣S∣个后缀,即对(S[i...∣S∣],i∈[1,N])(S[i...|S|],i\in[1,N])(S[i...∣S∣],i∈[1,N])都建立一个后缀自动机。构造过程中,每添加一个字符,根据后缀连接树的性质,从i到当前字符的子串数量为len(u)−len(link(u))len(u)-len(link(u))len(u)−len(link(u)),其中uuu为添加当前字符后的末状态。这样在构造iii到∣S∣|S|∣S∣时就可以求出区间[i,∣S∣][i,|S|][i,∣S∣]的所有子区间的子串数量。而枚举iii就可以求出[1,∣S∣][1,|S|][1,∣S∣]的所有子区间的子串数量了。

AC代码

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
const int MAXN = 2005;
int n;
char S[MAXN];
struct SAM {
int size, last;
struct Node {
int len, link;
int next[26];
void clear() {
len = link = 0;
memset(next, 0, sizeof(next));
}
} node[MAXN * 2];
void init() {
for (int i = 0; i < size; i++) {
node[i].clear();
}
node[0].link = -1;
size = 1;
last = 0;
}
void insert(char x) {
int ch = x - 'a';
int cur = size++;
node[cur].len = node[last].len + 1;
int p = last;
while (p != -1 && !node[p].next[ch]) {
node[p].next[ch] = cur;
p = node[p].link;
}
if (p == -1) {
node[cur].link = 0;
}
else {
int q = node[p].next[ch];
if (node[p].len + 1 == node[q].len) {
node[cur].link = q;
}
else {
int clone = size++;
node[clone] = node[q];
node[clone].len = node[p].len + 1;
while (p != -1 && node[p].next[ch] == q) {
node[p].next[ch] = clone;
p = node[p].link;
}
node[q].link = node[cur].link = clone;
}
}
last = cur;
}
}sam;
int Ans[MAXN][MAXN];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%s", S);
n = strlen(S);
//枚举i
for (int i = 0; i < n; ++i) {
sam.init();
int&& Temp = 0;
//添加字符
for(int j=i;j<n;++j){
sam.insert(S[j]);
Temp += sam.node[sam.last].len;
//如果存在后缀连接边
if (sam.node[sam.last].link != -1) {
Temp -= sam.node[sam.node[sam.last].link].len;
}
//编号从1开始
Ans[i + 1][j + 1] = Temp;
}
}
int Q;
scanf("%d", &Q);
while (Q--) {
int Left, Right;
scanf("%d%d", &Left, &Right);
printf("%d\n", Ans[Left][Right]);
}
}
return 0;
}

Reincarnation的更多相关文章

  1. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  2. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  3. 字符串(后缀自动机):HDU 4622 Reincarnation

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  4. 【HDU4622】Reincarnation(后缀自动机)

    [HDU4622]Reincarnation(后缀自动机) 题面 Vjudge 题意:给定一个串,每次询问l~r组成的子串的不同子串个数 题解 看到字符串的大小很小 而询问数太多 所以我们预处理任意的 ...

  5. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  6. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  7. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  8. 【HDU4622】Reincarnation

    [HDU4622]Reincarnation 一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了 #inc ...

  9. HDU4622 Reincarnation【SAM】

    HDU4622 Reincarnation 给出一个串,每次询问其一个子串有多少不同的子串 按每个后缀建立\(SAM\)不断往后加字符,然后记录答案,查询的时候直接用即可 //#pragma GCC ...

  10. hdu 4622 Reincarnation

    http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...

随机推荐

  1. join => innerJoin

    drupal7中 ->join == ->innerJoin 不是leftJoin哦 无语哦

  2. ERA5气压层数据驱动WRF的一些问题

    感谢Dawn的建议,兰溪之水的WRF教程 参考了一些经验,并结合实际后,成功用ERA5驱动WRF.实际上,用ERA5数据驱动WRF的方法和用ERA-Interim 数据驱动WRF极其类似. 总结几点是 ...

  3. apt-get update 报错 Repository ' InRelease' changed its 'Suite' value from 'stable' to 'oldstable'

    问题截图: 解决方案: apt-get update --allow-releaseinfo-change

  4. IDEA通过Spring Initalizr新建SSM (2)

    之前的方式是通过官网初始化demo(URL:https://start.spring.io/)现在记录一下通过IDEA自带的初始化器新建SSM框架 1.打开IDEA,点击新建,出现如下图菜单,点击Sp ...

  5. 做文件上传功能时,dubbo对MultipartFile文件传输时,一个bug:Fail to decode request due to: RpcInvocation

    三月 22, 2019 2:37:27 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...

  6. vue 生命周期个人理解

    activated():在vue对象存活的情况下,进入当前存在activated()函数的页面时,一进入页面就触发:可用于初始化页面数据等: created():在模板渲染成html前调用,即通常初始 ...

  7. node-sass低版本与高版本的node会有兼容性问题,导致安装失败

    node版本v14.18.1 node-sass版本v4.13.0 Python 2.7.18

  8. Java方法之稀疏数组

    稀松数组 当一个数组中大部分元素为0,或者为同一值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方式是: 1.记录数组一共有几行几列,有多少个不同值 2.把具有不同值的元素和行列及值记录在一 ...

  9. iis 无法在Web服务器上启动调试。打开的URL的IIS辅助进程当前没有运行

    今天开发又遇到老问题了,记录一下. 最近换了电脑 win10+vs 2010+oracle11g 客户端. IIS 发布的项目,开始是 把localhost 换成ip地址 总是报 iis 无法在Web ...

  10. Jmeter 5.0 遇见connection reset问题

    问题:大并发时遇见java.net.SocketException: Connection reset 测试过程中经常遇见connection reset ,原因是大数据量发送时,服务器不能接纳那么多 ...