SPOJ NSUBSTR Substrings 后缀自动机
来源:互联网 发布:移动2g网络的上传速度 编辑:程序博客网 时间:2024/06/10 04:09
题目大意:
就是现在给出一个长度不超过25W的字符串S, 定义F(x)表示字符串S中长度为x的子串出现的最多次数, 例如“ababa"中F(1) = 3 (“a”出现了3次), F(2) = 2 ("ab“或者"ba"都出现了2次)
F(3) = 2 ("aba"出现了2次) F(4) = F(5) = 1, 对于给出的字符串S, 长度为n, 输出F(1~n)的值
大致思路:
首先对于字符串S建立后缀自动机, 然后由于每个状态 p 所代表的子串是一个区间长度[Min(p), Max(p)], 出现次数是状态p的Right集合大小, 那么在处理处后缀自动机之后, 问题变成了, 对于n次更新, 每次区间[Min(p), Max(p)]中比Right(p)小的数变成Right(p), 最后区间[1, n]中的值就是F[1~n], 但是这样的更新比较麻烦, 考虑Parent树的性质, 父亲节点的为par, 子节点是p的话, 那么par的right集合一定大于p的right集合, 并且Max(par) = Min(p) + 1, 所以对于上面的[Min(p), Max(p)]的更新可以就当做是[1, Max(p)]的更新, 这样用dp[i]表示结果F[i]的话, 对于n次更新, 每次更新dp[Max(p)]的值, 最后dp[i ~ n]中的最大值就是F[i], 复杂度O(length(S))
代码如下:
Result : Accepted Memory : 68608 ms Time : 260 ms
/* * Author: Gatevin * Created Time: 2015/4/11 12:51:01 * File Name: Rin_Tohsaka.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl#define maxn 250010*2#define maxm 250010struct Suffix_Automation{ struct State { State *par; State *go[26]; int val, mi, cnt, right; void init(int _val = 0) { par = 0, val = _val, cnt = mi = right = 0; memset(go, 0, sizeof(go)); } int calc() { if(par == 0) return 0; return val - par->val; } }; State *root, *last, *cur; State nodePool[maxn]; State* newState(int val = 0) { cur->init(val); return cur++; } void initSAM() { cur = nodePool; root = newState(); last = root; } void extend(int w) { State *p = last; State *np = newState(p->val + 1); np->right = 1; while(p && p->go[w] == 0) { p->go[w] = np; p = p->par; } if(p == 0) { np->par = root; } else { State *q = p->go[w]; if(q->val == p->val + 1) { np->par = q; } else { State *nq = newState(p->val + 1); memcpy(nq->go, q->go, sizeof(q->go)); nq->par = q->par; q->par = nq; np->par = nq; while(p && p->go[w] == q) { p->go[w] = nq; p = p->par; } } } last = np; } int d[maxm]; State* b[maxn]; void topo() { int maxVal = 0; memset(d, 0, sizeof(d)); int cnt = cur - nodePool; for(int i = 1; i < cnt; i++) maxVal = max(maxVal, nodePool[i].val), d[nodePool[i].val]++; for(int i = 1; i <= maxVal; i++) d[i] += d[i - 1]; for(int i = 1; i < cnt; i++) b[d[nodePool[i].val]--] = &nodePool[i]; b[0] = root; return; } void SAMInfo() { State *p; int cnt = cur - nodePool; for(int i = cnt - 1; i > 0; i--) { p = b[i]; p->par->right += p->right; p->mi = p->par->val + 1; } return; } };Suffix_Automation sam;char s[maxm];int dp[maxn];int main(){ //scanf("%s", s); gets(s); int len = strlen(s); sam.initSAM(); for(int i = 0; i < len; i++) sam.extend(s[i] - 'a'); sam.topo(); sam.SAMInfo(); int cnt = sam.cur - sam.nodePool; memset(dp, 0, sizeof(dp)); for(int i = cnt - 1; i > 0; i--)//区间更新[1, b[i]->val]的话记录dp[b[i]->val]的最大值 dp[sam.b[i]->val] = max(dp[sam.b[i]->val], sam.b[i]->right); for(int i = len - 1; i > 0; i--)//数组后缀dp[i ~ len]中的最大值就是dp[i] dp[i] = max(dp[i], dp[i + 1]); for(int i = 1; i <= len; i++) printf("%d\n", dp[i]); return 0;}
0 0
- SPOJ NSUBSTR Substrings 后缀自动机
- SPOJ - NSUBSTR Substrings 后缀自动机
- spoj 8222 Substrings(NSUBSTR),后缀自动机
- SPOJ 8222 NSUBSTR - Substrings (后缀自动机)
- 【后缀自动机】 SPOJ NSUBSTR
- SPOJ NSUBSTR(后缀自动机)
- SPOJ 8222 NSUBSTR 后缀自动机
- 后缀自动机1002 SPOJ NSUBSTR
- SPOJ NSUBSTR 后缀自动机+DP
- SPOJ Substrings --后缀自动机
- spoj Substrings【后缀自动机】
- SPOJ NSUBSTR(Substrings-后缀自动机统计串出现次数-Right集合&Parent树の暴走)
- SPOJ 题目 8222 NSUBSTR - Substrings(后缀自动机+DP求子串出现最大次数)
- [SPOJ8222]NSUBSTR - Substrings(后缀自动机)
- spoj 8222 Substrings (后缀自动机)
- spoj 8222 substrings 【后缀自动机】
- SPOJ 8222 NSUBSTR Substrings
- SPOJ NSUBSTR Substrings
- The typical features of the film faced plywood
- ubuntu 内核源码下载及编译
- UINavigationController的工作原理
- STM32非对称PWM模式实现动态移相(Asymmetric PWM mode的用法)
- 阶乘的因式分解(一)
- SPOJ NSUBSTR Substrings 后缀自动机
- 有趣的if-else
- MagicalRecord入门教程
- HDU1568(Fobonacci公式)
- BAT CMD 批处理文件脚本总结(中文)
- java实现发送邮件可多个附件内嵌图片-commons-email使用
- 第5周-项目1-深复制体验
- The typical features of the film faced plywood
- java长字符串变短串工具类