Problem Description
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
 
 

题意: 给一个字符串长度最大2000,给出Q个查询[l,r]包含多少种连续的子串。
解析: 后缀自动机轻松过,先对查询离线排序,对于左端点相同的建立一个后缀自动
机,字符串长度最大只有2000,所以最后只用建2000个,每个sam插入的字符最多也就
2000,时间肯定是够的。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
struct SAM
{
int ch[maxn][];
int pre[maxn],step[maxn];
int last,id;
void init()
{
last=id=;
memset(ch[],-,sizeof(ch[]));
pre[]=-; step[]=;
}
void Insert(int c) //字符转化为数
{
int p=last,np=++id;
step[np]=step[p]+;
memset(ch[np],-,sizeof(ch[np]));
while(p!=-&&ch[p][c]==-) ch[p][c]=np,p=pre[p];
if(p==-) pre[np]=;
else
{
int q=ch[p][c];
if(step[q]!=step[p]+)
{
int nq=++id;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
step[nq]=step[p]+;
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while(p!=-&&ch[p][c]==q) ch[p][c]=nq,p=pre[p];
}
else pre[np]=q;
}
last=np;
}
int GetCnt()
{
int ret=;
for(int i=id;i>=;i--) ret+=step[i]-step[pre[i]];
return ret;
}
}sam;
char S[maxn/];
struct Ques
{
int l,r,id;
Ques(int l=,int r=,int id=):l(l),r(r),id(id){}
bool operator < (const Ques& t) const
{
if(l!=t.l) return l<t.l;
return r<t.r;
}
}q[];
int Q,ans[];
void solve()
{
sort(q,q+Q);
int last;
for(int i=;i<Q;i++)
{
if(i==||q[i].l!=q[i-].l)
{
sam.init();
last=q[i].l;
}
for(;last<=q[i].r;last++) sam.Insert(S[last]-'a');
ans[q[i].id]=sam.GetCnt();
}
for(int i=;i<Q;i++) printf("%d\n",ans[i]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",S+);
int len=strlen(S);
scanf("%d",&Q);
int l,r,id;
for(int i=;i<Q;i++)
{
scanf("%d%d",&l,&r);
q[i]=Ques(l,r,i);
}
solve();
}
return ;
}

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. 【HDU4622】Reincarnation(后缀自动机)

    [HDU4622]Reincarnation(后缀自动机) 题面 Vjudge 题意:给定一个串,每次询问l~r组成的子串的不同子串个数 题解 看到字符串的大小很小 而询问数太多 所以我们预处理任意的 ...

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

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

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

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

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

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

随机推荐

  1. 常用cl命令参数解释

    紧接前文,第一行cl命令如下: 1> cl /c /IC:\...\include /ZI /nologo- /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG ...

  2. Java编程思想-第四章练习题

    练习1:写一个程序,打印从1到100的值 public class Print1To100{ public static void main(String args[]){ for(int i = 1 ...

  3. SqlServer 挂载本地盘符到服务器端,方面备份还原

    --此脚本要在登录上SqlServer远程服务器后才能执行,把数据库备份到本地(内网使用) --(例如我用本机SqlServer客户端连接上192.168.3.12服务器时,执行脚本,可以把指定数据库 ...

  4. Nginx各个配置块功能详解

    Nginx学习笔记-入门篇 nginx初探 ginx服务器是轻量级web服务器中广受好评的一款产品,常用功能有HTTP代理与反向代理(目前已支持七层与四层代理),负载均衡,web缓存. nginx配置 ...

  5. C语言——文件

    需要理解的知识点:数据流.缓冲区.文件类型.文件存取方式   C语言可以将相关定义的数据按照内存的原样写入文件,这对于大规模数据来说比较方便,因为文件的尺寸比单纯的ASCII存储要小很多. 一.文件 ...

  6. USB设备架构

    USB设备,分为3层:1.底层为传送和接受数据的总线接口:2.中间层处理总线和不同端点的数据传输:3.最高层由串行总线设备提供.主要研究中间层. USB设备状态,主要研究外部可见状态. 1,连接态-- ...

  7. 网页CSS2

    列表与方块 width , hight (top, bottom ,left , right)   只有在决对坐标下才起作用 下面的使用与 ol  ul list-style:none // 取消序号 ...

  8. oracle rac 数据库常用命令

    oracle rac 数据库常用命令:1.所有实例和服务的状态srvclt status database -d orcl单个实例的状态:srvctl status instance -d orcl ...

  9. C++中的dynamic_cast和static_cast

    代码: #include <cstdio> #include <iostream> using namespace std; class A{ public: virtual ...

  10. Intellij idea配置springMvc4.2.6

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. 环境: Intellij iead 2016.1 java version " ...