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 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
随机推荐
- noi 45 金币
noi 45 金币 1.描述 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天)里,每天收到两枚金币:之后三天(第四.五.六天)里,每天收到三枚金币:之后四天( ...
- [Oracle19C ASM管理] ASM服务的启停
自动方式启停 crsctl stat res -t 查看ASM服务的状态,it's ok that ora.ons和ora.diskmon是OFFLINE [grid@centos7-19c.loca ...
- pip更改为国内源
1. 查看现有默认pip安装源 pip config list 2. 按次修改 添加指定源 pip install numpy -i https://pypi.tuna.tsinghua.edu.cn ...
- CDO学习1 CDO简介
参考自如下网站 http://www.ceda.ac.uk/static/media/uploads/ncas-reading-2015/cdo.pdf 介绍 一个有几百种操作符的单独命令 CDO受N ...
- .Net Core 中使用NLog替代默认日志
1.添加引用nlog.config和Nlog.Web.AspNetCore 2.配置NLog 配置文件 添加一个Web配置文件xxxx.Config <?xml version="1. ...
- mac 查看本机ip
创建 ip.sh 文件,文件内容为 ifconfig -a | grep -e "inet[^6]" |\ sed -e "s/.*inet[^6][^0-9]*\([0 ...
- 19c 滚动升级
一.环境说明 节点1 节点2 IP 192.168.3.77 192.168.3.88 DB 版本 Oracle 19.6.0.0.0 Oracle 19.6.0.0.0 OS 主机名 oadb1 ...
- 理解cpu过高的逻辑思维与分析方法
工作经常碰到负载过高,cpu占有太高,系统变慢,运维通常做的第一件事就是通过top或者uptime命令来了解系统负载的情况 通常uptime后会出现三个小数,就是平均负载值,那真正的了解这个平均负载值 ...
- fastadmin添加自定义按钮
问题:关于fastadmin框架列表页面自定义按钮功能 案例:自定义一个同步数据的按钮 <a href="javascript:;" class="btn btn- ...
- 手把手教你用Typora自动上传到picgo图床【教程与排坑】
手把手教你用Typora自动上传到picgo图床[教程与排坑] 参考链接: 1. https://blog.csdn.net/disILLL/article/details/104944710?utm ...