Hdu 4622 Reincarnation(后缀自动机)
/*
字符串长度较小, 可以离线或者直接与处理所有区间的答案
动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变
所以只统计这个点的贡献就好了
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define ll long long
#define M 4040
#define mmp make_pair
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int len[M], fa[M], ch[M][26], lst, cnt;
int ans[2020][2020];
char s[M];
void init()
{
memset(len, 0, sizeof(len));
memset(ch, 0, sizeof(ch));
memset(fa, 0, sizeof(fa));
lst = cnt = 1;
}
void insert(int c)
{
int p = ++cnt, f = lst;
lst = p;
len[p] = len[f] + 1;
while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
if(!f) fa[p] = 1;
else
{
int q = ch[f][c];
if(len[q] == len[f] + 1) fa[p] = q;
else
{
int nq = ++cnt;
fa[nq] = fa[q];
memcpy(ch[nq], ch[q], sizeof(ch[nq]));
len[nq] = len[f] + 1;
fa[p] = fa[q] = nq;
while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
}
}
}
int main()
{
int t = read();
while(t--)
{
//Init();
scanf("%s", s + 1);
int n = strlen(s + 1);
for(int i = 1; i <= n; i++)
{
init();
for(int j = i; j <= n; j++) insert(s[j] - 'a'), ans[i][j] = ans[i][j - 1] + len[lst] - len[fa[lst]];
}
int q = read();
while(q--)
{
int l = read(), r = read();
cout << ans[l][r] << "\n";
}
}
return 0;
}
Hdu 4622 Reincarnation(后缀自动机)的更多相关文章
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- HDU 4622 Reincarnation 后缀自动机
模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- hdu 4622 Reincarnation SAM模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...
- HDU 6583 Typewriter(后缀自动机)
Typewrite \[ Time Limit: 1500 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),现在你需要构造出这个字符串,你每次可 ...
随机推荐
- Qt开发问答
Qt开发问答 1, Difference between Dialog and widget and QMainWindow http://www.qtcentre.org/threads/3465- ...
- [转]一致性hash算法 - consistent hashing
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...
- Linux基础入门之vsFTP+MySQL/MariaDB认证实现虚拟用户配置详解
https://www.dwhd.org/20150603_144841.html 摘要 VSFTP可以使用系统账户或者匿名账户登录,但是出于安全的考虑,通常建议使用vsftp虚拟账户来登录ftp服务 ...
- 错误:php70w-common conflicts with php-common-5.3.3-49.el6.i686
记录一下 由于之前系统自带的php5.3.3没有卸载干净: 在执行phpize时报错说需要php-devel 然后yum -y install php-delel ; 然后就报错 错误:php70w ...
- 使用Vivado的block design
使用Vivado的block design (1)调用ZYNQ7 Processing System (2)配置ZYNQ7系统 (3)外设端口配置 根据开发板原理图MIO48和MIO49配置成了串口通 ...
- 关于div
<article id="a"> <div class="a1"></div> <div class="a2 ...
- Maven 之多模块构建
项目的打包类型:pom.jar.war 项目中一般使用maven进行模块管理,每个模块下对应都有一个pom文件,pom文件中维护了各模块之间的依赖和继承关系.项目模块化可以将通用的部分抽离出来,方便重 ...
- eclipse基于git上传项目到码云上
此文章只讲如何把项目上传到码云上面,前提是你的项目已经用git创建好了. 关于如何用git创建项目,请读者见我博客的另一篇文章. 继续:右键点击ssm [ssm master]项目-->选择Te ...
- C/C++基础----关联容器
基本属性 与顺序容器的差别,按照关键字来保存和访问,而顺序容器是按照容器中的位置来顺序保存和访问. map:每个元素是一对键值(key-valye)组合:set每个元素只包含关键字.. 每个根据关键字 ...
- PAT 乙级 1078 字符串压缩与解压 (20)
文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 ...