【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]子串 ...
随机推荐
- PHP curl 常用操作
网页内容替换 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com"); //执行后 ...
- 一个客户端一键安装环境和服务的shell脚本
#!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...
- Go学习笔记02-源码
第二部分 源码 基于 Go 1.4,相关文件位于 src/runtime 目录.文章忽略了 32bit 代码,有兴趣的可自行查看源码文件.为便于阅读,示例代码做过裁剪. 1. Memory Alloc ...
- php用正则匹配出图片img标签中的src路径(兼容)
用php抓图片是个常用的需求,下面提供一个比较兼容的正则表达式来实现php抓取出页面.字符串中所有图片的src. 下面是一个范例,能匹配各种标签格式写法的图片,不管src在什么地方,还是单引号.双引号 ...
- 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList这个问题
今天使用SSM框架,用@ResponseBody注解,出现了这个问题 java.lang.IllegalArgumentException: No converter found for return ...
- Java经典编程题50道之二十三
给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. public class Example23 { public static void main(String[] arg ...
- ASP.NET Core的身份认证框架IdentityServer4--(4)添加第三方快捷登录
添加对外部认证的支持 接下来我们将添加对外部认证的支持.这非常简单,因为你真正需要的是一个兼容ASP.NET Core的认证处理程序. ASP.NET Core本身也支持Google,Facebook ...
- SpringMvc笔记-对RESTFUL风格的配置
1.@RequestMapping注解可以使用如下参数: 1,params:例如params={'username',"age!=100"}表示需要usernmame并且age 属 ...
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions报错解决办法
1.出现原因: 导入的uri由于不是正确的导致这个jstl不支持el的表达式 jstl uri导入错误: <%@ taglib prefix="c" uri=" ...
- Codeforces13E - Holes
Portal Description \(n(n\leq10^5)\)个洞排成一条直线,第\(i\)个洞有力量值\(a_i\),当一个球掉进洞\(i\)时就会被立刻弹到\(i+a_i\),直到超出\( ...