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 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
随机推荐
- C# DataGrid嵌套DataGrid动态隐藏显示行
前端代码: <Window x:Class="DataGridPractice.MainWindow" xmlns="http://schemas.microsof ...
- solt废弃,报错解决方法
1.饿了么组件库给出得文字提示框 写到项目里之后报错 提示 solt已经废弃 <el-tooltip placement="top"> <div slot=&qu ...
- powergui模块基本设置
Powergui模块可以显示系统稳定状态的电流和电压及电路(电感电流和电容电压)所有的状态变量值. 尤其是电力电子仿真中需要加入powergui模块,否则会报错. simulink仿真用到simpow ...
- 前端项目线上部署记录 | vue-cli
一.修改公开路径后打包;npm run build 新建一个vue.config.js文件,如果本地打开,则路径为"./',线上则'/',不加'.' module.exports = { p ...
- 解决-装了WPS后Windows无法预览word、Excel、PPT等的问题
https://www.bilibili.com/read/cv10469054/ https://www.cnblogs.com/qq3285862072/p/15097970.html Windo ...
- 不符合Json格式都能被Gson 转成对象使用!!!
问题描述: 在与前端测试的时候,前端下发商品档案,结构为{""items":[{A对象},{B对象},{C对象}]},他下发了504条数据,但我们插入数据库的时候 只有1 ...
- 在datagridview中首列添加复选框
//为dgv增加复选框列 DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn(); //列显示名称 checkbo ...
- VScode打开文件夹位置技巧
VScode在打开文件夹,弹出对话框的时候,去文件夹(应用)到达该路径,对话框中的路径自动变为当前文件夹(应用)的路径.去文件夹(应用)到达该路径
- Chtholly Tree 学习笔记
前言 珂朵莉树 (Chtholly Tree) 是一种简单优美的数据结构,就像 Chtholly 一样可爱.暴力即优美. 适用于一些有区间赋值操作的序列操作题. Chtholly Tree 的本质是把 ...
- 函数调用_通过apply和call方法调用
不同类型函数调用之间的主要区别在于:最终作为函数上下文(可以通过this参数隐式引用到)传递给执行函数对象不同.对于方法而言,即为所在的对象:对于函数而言是window或是undefined(取决于是 ...