Reincarnation
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的更多相关文章
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
- 字符串(后缀自动机):HDU 4622 Reincarnation
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- 【HDU4622】Reincarnation(后缀自动机)
[HDU4622]Reincarnation(后缀自动机) 题面 Vjudge 题意:给定一个串,每次询问l~r组成的子串的不同子串个数 题解 看到字符串的大小很小 而询问数太多 所以我们预处理任意的 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- 【HDU4622】Reincarnation
[HDU4622]Reincarnation 一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了 #inc ...
- HDU4622 Reincarnation【SAM】
HDU4622 Reincarnation 给出一个串,每次询问其一个子串有多少不同的子串 按每个后缀建立\(SAM\)不断往后加字符,然后记录答案,查询的时候直接用即可 //#pragma GCC ...
- hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
随机推荐
- The first python article
Smile is the most beautiful language! and Python so on !
- html元素全屏展示
参数传入dom对象即可,注意不是jQuery对象,Vue下兼容 /** * 面板全屏展示 */ fullscreen: function () { if (this.isFullScreen) { / ...
- Android组件化开发-----页面路由(ARouter)
平时开发中,我们经常用到页面跳转功能.之前我一直使用Intent过跳转 Intent intent = new Intent(A.this, B.class); intent.putExtra(&qu ...
- Android拍照程序适配
public void takePic(){ String forderPath = getExternalFilesDir("") + "/pic"; Fil ...
- rar 压缩解压
rar wget https://www.rarlab.com/rar/rarlinux-x64-612.tar.gz # 压缩文件 rar a -r test.rar file # 解压文件 unr ...
- matplotlib 中文乱码的解决方法
关于报错信息 Glyph 26426 missing from current font. 这个错误的原因是:本地没有可支持中文字体显示的配置文件,所以第一步需要先去下载相关的配置文件. 下载链接 h ...
- shopt 内置命令启用shell选项 (extglob)
使用shopt 内置命令启用shell选项 (extglob) 则会识别几个扩展模式匹配运算符 模式列表是由 | 分割 查看shell选项 extglob shopt |grep extglob 启动 ...
- ONOS中新建分支并关联远程库
新建分支并关联远程库 廖雪峰学习git教程网站:(多人协作) https://www.liaoxuefeng.com/wiki/896043488029600/900375748016320 git远 ...
- b站
题目描述 n条鱼,每条鱼的体积是ai 每一轮,每一条鱼一定会吃掉右边比自己小的第一条鱼,一条鱼只能被吃一次. 多少轮后,鱼的数量会稳定. 例子: 6 6 3 3 --> 6 6 3(第二个3)- ...
- pdf在线预览 ng2-pdf-viewer的运用
angular项目在线预览PDF 1 安装 ng2-pdf-viewer yarn add ng2-pdf-viewer 2 在项目中添加 import { NgModule } from '@ang ...