HDU 4622 Reincarnation
2015-09-07 22:51
295 查看
Reincarnation
Time Limit: 3000msMemory 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
相关文章推荐
- BT的文件系统btrfs
- linux--SSH Secure Shell Client
- HDU2.2.7 Train Problem II
- 数学概率问题
- CTabCtrl - 如何使用TabCtrl控件
- Android软件安全开发实践(下)
- 同一进程中的线程究竟共享哪些资源
- 四种异步更新Android的方式
- 对数据库中数据模型的认识
- SetTimeOut 详解
- UVA 11987 Almost Union-Find (单点修改的并查集)
- 关于那些最好玩的户外APP合集下(适合资深驴友、牛逼设计狮、装逼攻城狮)
- 《机器学习实战》笔记之五——Logistic回归
- MySQL(十六)之使用Mysql-Proxy实现MySQL的读写分离
- react-native试玩(11)-模态
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
- 练习题
- 2015第37周一
- Linux-remote change password (more)
- js整理2