hdu 4622
来源:互联网 发布:js调用接口获取数据 编辑:程序博客网 时间:2024/05/22 08:09
Reincarnation
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)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.
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.
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
2bbaba53 42 22 52 41 4baaba53 33 41 43 55 5
Sample Output
3175813851HintI won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
后缀自动机要理解其含义,从起点到每个点的不同路径,就是不同的子串。
到每一个点,不同路径,其实就是以这个点为最后一个字符的后缀,长度是介于(p->fa->len,p->len]之间的,个数也就清楚了。
而且这个其实是动态变化的,每加入一个字符,就可以知道新加了几个不同子串。
加个pos,记录位置,这样就很容易预处理了。
两篇介绍后缀自动机的博客:http://blog.sina.com.cn/s/blog_7812e98601012cim.html
http://blog.sina.com.cn/s/blog_8fcd775901019mi4.html
AC:(copy别人的)
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int CHAR = 26;const int MAXN = 2020;struct SAM_Node{ SAM_Node *fa,*next[CHAR]; int len; int id,pos; SAM_Node(){} SAM_Node(int _len) { fa = 0; len = _len; memset(next,0,sizeof(next)); }};SAM_Node SAM_node[MAXN*2], *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 = 0; SAM_root = SAM_last = newSAM_Node(0); SAM_node[0].pos = 0;}void SAM_add(int x,int len){ SAM_Node *p = SAM_last, *np = newSAM_Node(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 + 1) { np->fa = q; return; } SAM_Node *nq = newSAM_Node(q); nq->len = p->len + 1; q->fa = nq; np->fa = nq; for(;p && p->next[x] == q;p = p->fa) p->next[x] = nq;}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,0,sizeof(Q)); for(int i = 0;i < n;i++) { SAM_init(); for(int j = i;j < n;j++) { SAM_add(str[j]-'a',j-i+1); } for(int j = 1;j < SAM_size;j++) { Q[i][SAM_node[j].pos-1+i]+=SAM_node[j].len - SAM_node[j].fa->len; } for(int j = i+1;j < n;j++) Q[i][j] += Q[i][j-1]; } int M; int u,v; scanf("%d",&M); while(M--) { scanf("%d%d",&u,&v); u--;v--; printf("%d\n",Q[u][v]); } } return 0;}
0 0
- hdu 4622
- HDU 4622 Reincarnation
- hdu 4622(后缀自动机)
- HDU-4622-Reincarnation
- hdu 4622Reincarnation
- HDU 4622 Reincarnation SAM
- HDU 4622 Reincarnation
- HDU 4622 Reincarnation
- HDU 4622 后缀自动机
- hdu 4622 后缀数组
- HDU 4622 Reincarnation
- hdu 4622 Reincarnation
- HDU 4622 后缀自动机
- hdu 4622 Reincarnation
- hdu
- hdu
- HDU
- hdu ()
- 程序员应该常去逛逛的地方(技术社区)
- 微软Madoko:在线的类似CSDN markdown但是功能更强的编辑器
- 编写高质量iOS代码的方法总结
- 常用ui组件总结
- iOS开发之UI篇第二弹
- hdu 4622
- 关于在ubuntu下eclipse中c++11支持和编译运行c++11报错的解决方法
- 从无到有:微信后台系统的演进之路
- 单元测试学习一
- redis主从集群搭建及容灾部署
- 关于Excel中的一些发现
- c++ string trim
- Android内存泄露情况总结
- 3--mysql学习笔记