【HDU4622】Reincarnation(后缀自动机)
【HDU4622】Reincarnation(后缀自动机)
题面
Vjudge
题意:给定一个串,每次询问l~r组成的子串的不同子串个数
题解
看到字符串的大小很小
而询问数太多
所以我们预处理任意的答案
枚举左端点,依次向右加入新节点
直接统计答案即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 2222
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int ans[MAX][MAX];
int last,tot;
char ch[MAX];
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
void init()
{
last=tot=1;
memset(t,0,sizeof(t));
}
void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[np].ff=t[q].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
int T=read();
while(T--)
{
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)
{
init();
for(int j=i;j<=l;++j)
{
extend(ch[j]-97);
ans[i][j]=ans[i][j-1]+t[last].len-t[t[last].ff].len;
}
}
int Q=read();
while(Q--)
{
int l=read(),r=read();
printf("%d\n",ans[l][r]);
}
}
return 0;
}
【HDU4622】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 ...
- HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给一个字符串,询问某字串的不同字串的个数. 可以用后缀数组来解决,复杂度O(n).先求出倍 ...
- HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)
Problem Description Now you are back,and have a task to do: Given you a string s consist of lower-ca ...
- [hdu4622 Reincarnation]后缀数组
题意:给一个长度为2000的字符串,10000次询问区间[L,R]内的不同子串的个数 思路:对原串的每个前缀求一边后缀数组,询问[L,R]就变成了询问[L,n]了,即求一个后缀里面出现了多少个不同子串 ...
- Hdu 4622 Reincarnation(后缀自动机)
/* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
随机推荐
- LeetCode - 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- yum 安装 nfs,rpcbind 出现错误 libc.so.6(GLIBC_2.14)(64bit) is needed by
错误信息: Running rpm_check_debugERROR with rpm_check_debug vs depsolve:libc.so.6(GLIBC_2.14)(64bit) is ...
- docker数据库
拉取镜像 # docker pull mysql: 创建docker数据库容器 # docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASS ...
- slf4j-logback 日志以json格式导入ELK
同事整理的,在此分享.logback,log4j2 等slf4j的日志实现都可以以json格式输出日志, 这里采用的是logback.当然也可以以文本行的格式输出,然后在logstash里通过grok ...
- 深入研究Sphinx的底层原理和高级使用
深入研究Sphinx的底层原理和高级使用
- UVA-818 dfs + 位运算
暴力枚举一些圆环,将这些圆环解开,看能否成为单链.判断单链的三个条件: 除了这些删除的圆环之外,其他圆环还连接着的圆环不能超过两个. 剩下的环没有连成圈. 剩下的圆环共分成m堆,每堆之间无连接,m必须 ...
- XOR (莫队)
Time Limit: 2000 ms Memory Limit: 256 MB Description 给定一个含有n个整数的序列 a1, a2,..., an. 定义 f(x,x) = a[x ...
- [Code] 递归函数在函数式 Java 中的实现
这里以阶乘函数为例,对于阶乘函数 fact :: Integer -> Integer fact 0 = 1 fact n = n * fact (n - 1) 在函数式 Java 中可以使用 ...
- yaf框架学习文件配置
文件配置: 在配置php支持yaf的时候,可以设置一个参数yaf.environ:把本地开发设置成develop.测试环境配置成test.生产环境配置成product. [yaf] extension ...
- scrapy安装的问题
Found existing installation: six 1.4.1 DEPRECATION: Uninstalling a distutils installed project (six) ...