hdu 4622 Reincarnation
来源:互联网 发布:劳动收入份额数据 编辑:程序博客网 时间:2024/06/07 13:48
原题链接.
题目大意:
给出一个字符串。
求它的一个子串的不同子串有多少个。
多组询问。
1<=n<=2000
题解:
显然可以离线。
枚举左端点,往后用后缀自动机直接搞就行了。
但是有个问题:
如何知道每次新加了多少条路径?
注意到对一个状态有一个性质:
root到状态x的路径长度在(step[parent[x]],step[x]]这个范围。
而且好像区间内每个长度的路径数只有一条,这个我还没有搞明白。
Code:
#include<cstdio> #include<cstring>#define fo(i, x, y) for(int i = x; i <= y; i ++)#define mem(a) memset(a, 0, sizeof a)using namespace std;const int N = 4005;int ss[N / 2][N / 2];struct suffix_autonmation { char s[N]; int sum, tot, last, pre[N], son[N][26], step[N]; #define push(v) step[++ tot] = v void Extend(int c) { push(step[last] + 1); int p = last, np = tot; for(; p && !son[p][c]; p = pre[p]) son[p][c] = np; if(!p) pre[np] = 1; else { int q = son[p][c]; if(step[p] + 1 < step[q]) { push(step[p] + 1); int nq = tot; memcpy(son[nq], son[q], sizeof son[q]); pre[nq] = pre[q]; pre[q] = pre[np] = nq; for(; son[p][c] == q; p = pre[p]) son[p][c] = nq; } else pre[np] = q; } last = np; sum += step[np] - step[pre[np]]; } void Build() { scanf("%s", s + 1); int n = strlen(s + 1); fo(i, 1, n) { mem(son); mem(pre); mem(step); sum = 0; last = tot = 1; fo(j, i, n) { Extend(s[j] - 'a'); ss[i][j] = sum; } } }} suf;int T, Q, x, y;int main() { for(scanf("%d", &T); T; T --) { suf.Build(); for(scanf("%d", &Q); Q; Q --) scanf("%d %d", &x, &y), printf("%d\n", ss[x][y]); }}
阅读全文
1 0
- HDU 4622 Reincarnation
- HDU-4622-Reincarnation
- hdu 4622Reincarnation
- HDU 4622 Reincarnation SAM
- HDU 4622 Reincarnation
- HDU 4622 Reincarnation
- HDU 4622 Reincarnation
- hdu 4622 Reincarnation
- hdu 4622 Reincarnation
- hdu 4622 Reincarnation (后缀自动机)
- hdu 4622 Reincarnation (后缀自动机)
- hdu 4622 Reincarnation(SAM)
- HDU 4622 Reincarnation 后缀自动机
- hdu 4622 Reincarnation(hash)
- hdu 4622 Reincarnation(后缀数组)
- [后缀数组+枚举] hdu 4622 Reincarnation
- hdu 4622 Reincarnation(后缀数组)
- hdu 4622 Reincarnation(后缀树组求子串个数)
- 数据结构实验之查找四:二分查找
- jQuery登录验证
- 夜晚接到了下班的码农
- Python中必须掌握的常用字符串处理函数
- 敏感字自动过滤
- hdu 4622 Reincarnation
- Leetcode 300 Longest Increasing Subsequence
- 1065. 单身狗(25)
- 精通python网络爬虫学习笔记(1)
- 斐波那契数列的递归及非递归
- 200. Number of Islands(DFS or BFS)
- macOS使用django安装mysqlclient遇到的问题(mysqlclient 1.3.3 or newer is required)
- leeetcode 400 Nth Digit
- MUSTer:Multi-Store Tracker:A Cognitive Psychology Inspired Approach to Object Tracking