您的位置:首页 > 其它

HDU 4622 Reincarnation

2015-09-07 22:51 295 查看

Reincarnation

Time Limit: 3000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 4622
64-bit integer IO format: %I64d Java class name: Main

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

2013 Multi-University Training Contest 3

解题:据说是CLJ的题

后缀自动机求某个区间子串的个数

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
int ret;
struct node {
int f,len,son[26];
void init() {
memset(son,-1,sizeof son);
len = 0;
f = -1;
}
};
struct SAM {
node e[maxn<<1];
int tot,last;
int newnode(int len = 0) {
e[tot].init();
e[tot].len = len;
return tot++;
}
void init() {
tot = last = 0;
newnode();
}
void extend(int c) {
int p = last,np = newnode(e[p].len + 1);
while(p != -1 && e[p].son[c] == -1) {
e[p].son[c] = np;
p = e[p].f;
}
if(p == -1) {
e[np].f = 0;
ret += e[np].len - e[0].len;
} else {
int q = e[p].son[c];
if(e[p].len + 1 == e[q].len) {
e[np].f = q;
ret += e[np].len - e[q].len;
} else {
int nq = newnode();
e[nq] = e[q];
e[q].f = e[np].f = nq;
e[nq].len = e[p].len + 1;
ret += e[np].len - e[e[np].f].len;
while(p != -1 && e[p].son[c] == q) {
e[p].son[c] = nq;
p = e[p].f;
}
}
}
last = np;
}
} sam;
int ans[2010][2010];
char str[maxn];
int main() {
int kase,q,L,R;
scanf("%d",&kase);
while(kase--) {
scanf("%s",str);
for(int i = 0; str[i]; ++i) {
sam.init();
ret = 0;
for(int j = i; str[j]; ++j) {
sam.extend(str[j] - 'a');
ans[i][j] = ret;
}
}
scanf("%d",&q);
while(q--) {
scanf("%d%d",&L,&R);
printf("%d\n",ans[L-1][R-1]);
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: