【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(后缀自动机)的更多相关文章

  1. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  2. HDU 4622 Reincarnation 后缀自动机

    模板来源:http://blog.csdn.net/zkfzkfzkfzkfzkfzkfzk/article/details/9669747 解法参考:http://blog.csdn.net/dyx ...

  3. HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给一个字符串,询问某字串的不同字串的个数. 可以用后缀数组来解决,复杂度O(n).先求出倍 ...

  4. HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)

    Problem Description Now you are back,and have a task to do: Given you a string s consist of lower-ca ...

  5. [hdu4622 Reincarnation]后缀数组

    题意:给一个长度为2000的字符串,10000次询问区间[L,R]内的不同子串的个数 思路:对原串的每个前缀求一边后缀数组,询问[L,R]就变成了询问[L,n]了,即求一个后缀里面出现了多少个不同子串 ...

  6. Hdu 4622 Reincarnation(后缀自动机)

    /* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...

  7. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  9. HDU 4622 Reincarnation(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...

随机推荐

  1. 机器学习实践之Logistic回归

        关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2017年12月17日 19:18:31所撰写内容(http://blog.cs ...

  2. sql的升阶

    前言:基本数据库操作根本无法满足实际的需要,需要引入更多的操作. 触发器-隐式的,主动的,更新数据表中的信息.带有inserted和deleted两个临时表,代表新操作和旧操作. 它是一种特殊的存储过 ...

  3. Websocket原理及使用场景[转载]

    WebSocket的使用场景 社交聊天.弹幕.多玩家游戏.协同编辑.股票基金实时报价.体育实况更新.视频会议/聊天.基于位置的应用.在线教育.智能家居等需要高实时的场景 由轮询到WebSocket 1 ...

  4. TJ4运行环境

    http://springwq2011.blog.51cto.com/4332889/966028 http://blog.csdn.net/u013573789/article/details/45 ...

  5. Ubuntu Linux 与 Windows 7双系统安装教程(图文)

    前期准备: 1. 备份你的重要数据到其他设备上,以防不测2. 准备linux镜像.可以到ubuntu官网下载iso格式的文件.(注意:如果你是双显卡,不要随便下.amd的双显卡请选择含有"a ...

  6. POJ 1021 2D-Nim

    Description The 2D-Nim board game is played on a grid, with pieces on the grid points. On each move, ...

  7. ajaxfileupload批量上传文件+图片尺寸限制

    1.首先展示ajaxfileupload代码,在这里修改为批量上传 //ajaxfileupload不展示全部代码,这是修改前与修改后代码对比,目的是上传多个文件 createUploadForm: ...

  8. PPPoE拨号流程

    PPPoE(Point to Point Protocol over Ethernet,基于以太网的点对点协议)的工作流程包含发现(Discovery)和会话(Session)两个阶段,发现阶段是无状 ...

  9. Java经典编程题50道之二十九

    求一个3*3矩阵对角线元素之和. public class Example29 {    public static void main(String[] args) {        int[][] ...

  10. UVALive - 3938 (线段树,区间查询)

    思路:详细分析见训练指南.注意可能答案的起点在左区间,终点在右区间 AC代码 #include <stdio.h> #include <algorithm> using nam ...