HDU 4622 后缀自动机
2014-07-26 23:27
288 查看
Reincarnation
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2009 Accepted Submission(s): 680
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test cases,for each query,print the answer in one line.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
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.
[align=left]Source[/align]
2013 Multi-University Training Contest 3
给定一个长度为2000的字符串,访问任意一区间不同字串个数。
构造2000次后缀自动机,然后把结果预处理,O(1)回答。
第一次照着写,模糊的理解了一点。
代码:
/* ***********************************************
Author :rabbit
Created Time :2014/7/26 22:53:41
File Name :6.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int CHAR=26;
const int MAXN=2200;
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(p->len+1);
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;
}
void SAM_build(char *s){
SAM_init();
int len=strlen(s);
for(int i=0;i<len;i++)
SAM_add(s[i]-'a',i+1);
}
int Q[3000][3000];
char str[3000];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
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;
scanf("%d",&M);
while(M--){
int u,v;
scanf("%d%d",&u,&v);
u--;v--;
printf("%d\n",Q[u][v]);
}
}
return 0;
}
相关文章推荐
- hdu 4622 求区间不同子串数 后缀数组|后缀自动机|字符串hash
- hdu 4622 Reincarnation (后缀自动机)
- HDU 4622 Reincarnation(后缀自动机)
- hdu 4622 Reincarnation(后缀自动机,入门级)
- HDU 4622 多校第三场1002 后缀自动机
- hdu 4622(后缀自动机|后缀数组)
- HDU 4622 Reincarnation (区间不相同子串个数:字符串哈希 | 后缀数组 | 后缀自动机)
- HDU 4622 多校第三场1002 后缀自动机
- HDU - 4622 Reincarnation 后缀自动机
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
- HDU 4622 Reincarnation(后缀自动机)
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
- hdu 4622 后缀数组计数问题||后缀自动机
- hdu 4622 Reincarnation (后缀自动机)
- [后缀自动机 模板题 || 字符串Hash] HDU 4622 Reincarnation
- Hdu 4622 Reincarnation 后缀数组/后缀自动机
- HDU 4622 Reincarnation 后缀自动机
- HDU 4622 Reincarnation 后缀数组 或 后缀自动机
- 字符串(后缀自动机):HDU 4622 Reincarnation
- HDU 4622 (后缀自动机)