Reincarnation

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 843    Accepted Submission(s): 283

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

Hint

I won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.

 
Source
 
Recommend
zhuyuanchen520
 

刚刚学了下后缀自动机,然后把这题先A掉。

这题就是查询一个区间内的不同子串的个数。

如果单单是求一个字符串的不同子串个数,无论是后缀数组还是后缀自动机都非常容易实现。

N<=2000.

我是用后缀自动机预处理出所有区间的不同子串个数。

建立n次后缀自动机。

后缀自动机要理解其含义,从起点到每个点的不同路径,就是不同的子串。

到每一个点,不同路径,其实就是以这个点为最后一个字符的后缀,长度是介于(p->fa->len,p->len]之间的,个数也就清楚了。

而且这个其实是动态变化的,每加入一个字符,就可以知道新加了几个不同子串。

加个pos,记录位置,这样就很容易预处理了。

学了一天的后缀自动,在世界冠军cxlove的博客中一直看SAM,终于有点理解了,Orz,太神奇了。

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std; const int CHAR = ;
const int MAXN = ;
struct SAM_Node
{
SAM_Node *fa,*next[CHAR];
int len;
int id,pos;
SAM_Node(){}
SAM_Node(int _len)
{
fa = ;
len = _len;
memset(next,,sizeof(next));
}
};
SAM_Node SAM_node[MAXN*], *SAM_root, *SAM_last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
SAM_node[SAM_size] = SAM_Node(len);
SAM_node[SAM_size].id = SAM_size;
return &SAM_node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
SAM_node[SAM_size] = *p;
SAM_node[SAM_size].id = SAM_size;
return &SAM_node[SAM_size++];
}
void SAM_init()
{
SAM_size = ;
SAM_root = SAM_last = newSAM_Node();
SAM_node[].pos = ;
}
void SAM_add(int x,int len)
{
SAM_Node *p = SAM_last, *np = newSAM_Node(p->len+);
np->pos = len;
SAM_last = np;
for(;p && !p->next[x];p = p->fa)
p->next[x] = np;
if(!p)
{
np->fa = SAM_root;
return;
}
SAM_Node *q = p->next[x];
if(q->len == p->len + )
{
np->fa = q;
return;
}
SAM_Node *nq = newSAM_Node(q);
nq->len = p->len + ;
q->fa = nq;
np->fa = nq;
for(;p && p->next[x] == q;p = p->fa)
p->next[x] = nq;
}
void SAM_build(char *s)
{
SAM_init();
int len = strlen(s);
for(int i = ;i < len;i++)
SAM_add(s[i] - 'a',i+);
} int Q[MAXN][MAXN];
char str[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
int n = strlen(str);
memset(Q,,sizeof(Q));
for(int i = ;i < n;i++)
{
SAM_init();
for(int j = i;j < n;j++)
{
SAM_add(str[j]-'a',j-i+);
}
for(int j = ;j < SAM_size;j++)
{
Q[i][SAM_node[j].pos-+i]+=SAM_node[j].len - SAM_node[j].fa->len;
}
for(int j = i+;j < n;j++)
Q[i][j] += Q[i][j-];
}
int M;
int u,v;
scanf("%d",&M);
while(M--)
{
scanf("%d%d",&u,&v);
u--;v--;
printf("%d\n",Q[u][v]);
}
}
return ;
}

HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)的更多相关文章

  1. HDU4622 (查询一段字符串的不同子串个数,后缀自动机)

    http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给出一个字符串和q次询问,每次询问[l,r]区间内不同子串的个数 分析: N<=2000. 我 ...

  2. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  3. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  4. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  5. 字符串(后缀自动机):HDU 4622 Reincarnation

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

  6. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

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

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

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

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

  9. hdu多校第一场 1006 (hdu6583)Typewriter dp/后缀自动机

    题意: 有个打字机,在当前字符串后新加一个字花费p,把当前字符串的一个连续子串拷贝到当前字符串的末尾花费q,给定一个字符串,求用打字机打出这个字符串的最小花费. 题解: 容易想到用dp 记dp[i]为 ...

随机推荐

  1. 基于Metronic的Bootstrap开发框架经验总结(11)--页面菜单的几种呈现方式

    在常规的后台管理系统或者前端界面中,一般都有一个导航菜单提供给用户,方便选择所需的内容.基于Metronic的Bootstrap开发框架,是整合了Metroinc样式,以及Boostrap组件模块的内 ...

  2. canvas孙悟空脚踩白云今年是猴年

    效果查看:http://hovertree.com/texiao/html5/30/ 使用HTML5的canvas画的孙悟空,CSS3画的白云飘飘. 刚擒住了几个妖 又降住了几个魔 魑魅魍魉怎么他就这 ...

  3. 离线安装swashbuckle(webapi自动文档及测试工具)

    1.找到已经成功安装过的项目根目录的packages文件夹拷贝到新的项目的根目录 2.vs设置nuget程序包源 将源:地址改为新项目的packages文件夹 3.重新编译并修改代码 右键项目-> ...

  4. discuz X3.1 源代码阅读,记录代码片段

    require_once libfile('function/post'); // /source/function/function_post.php require_once libfile('p ...

  5. PHP工作笔记:数组转字符串与字符串转数组

    一个数组要入库,发现不可以,特定用函数转为字符串入库 $data = array( 'http://img4.bitautoimg.com/autoalbum/files/20110420/734/2 ...

  6. php实现设计模式之 备忘录模式

    <?php /*备忘录模式:在不破坏封装的前提下,获取对象的内部状态,并且在对象外保存该状态.这样就可以将该对象恢复到保存之前的状态(行为模式) * * 发起人:记录当前时刻的内部状态,负责定义 ...

  7. [翻译]用 Puppet 搭建易管理的服务器基础架构(2)

    我通过伯乐在线翻译了一个Puppet简明教程,一共分为四部分,这是第二部分. 原文地址:http://blog.jobbole.com/87680/ 本文由 伯乐在线 - Wing 翻译,黄利民 校稿 ...

  8. 使用CruiseControl.Net全面实现持续集成

    持续集成想必大家很多人都听说过,甚至都实践过,最近我又一次亲历了一次持续集成,现将我的经验分享给大家.关于持续集成的理论在本文概不涉及,本文的主要目的是实战CruiseControl.Net,用它来全 ...

  9. Windows 10 的音频和 MIDI API将统一

    微软一统 Windows 10 的音频和 MIDI API 微软在夏季NAMM上的A3E大会上做了主题演讲,他们对Windows 10的音频和MIDI API都做了新的规划,开发者针对Windows ...

  10. 一句话知识:如何解决winform自动缩放产生的布局问题.

    转自http://www.cnblogs.com/KenBlove/articles/1281823.html有时候你会发现本来好好的WinForm程序在别的机器上显示的尺寸就不对了.这些问题主要发生 ...