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 ;
}

最新文章

  1. HTTP 错误 500.22 - Internal Server Error
  2. BIOS开启虚拟化
  3. [R]R下as.Date()函数的坑
  4. Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
  5. Https 公钥、私钥、证书
  6. java中基本输入输出流的解释(flush方法的使用)
  7. 利用URLScan工具过滤URL中的特殊字符(仅针对IIS6)-- 解决IIS短文件名漏洞
  8. CentOS6.3 Hostname设定修改
  9. poj3678
  10. Android多线程文件下载器
  11. Struts2---Result(传统Web应用程序与Ajax应用程序的异同)
  12. Dubbo实战快速入门 (转)
  13. 07 总结ProgressDialog 异步任务
  14. Linux 运维工作中的经典应用ansible(批量管理)Docker容器技术(环境的快速搭建)
  15. js写滚动的文字
  16. zabbix问题记录
  17. 2017-2018-2 20165316 实验三《敏捷开发与XP实践》实验报告
  18. [PY3]——threading.Event
  19. 身份证&amp;银行卡识别方案
  20. Web开发入门知识小总结

热门文章

  1. ActiveX(三)ActiveX 调用 Js
  2. jQuery获取Select选中的Text和Value
  3. 学习C#线程
  4. Chrome立体动画代码
  5. HashTable(散列表)
  6. Lind.DDD.ILogicDeleteBehavor~逻辑删除的实现
  7. [翻译]Apache Spark入门简介
  8. 字体属性设置(一):谷歌浏览器12px以下字体的显示;方法和原理
  9. request.getParameter(“参数名”) 中文乱码解决方法
  10. 使用jq插入节点