codeforces 432D Prefixes and Suffixes
由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀。另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了。先分别用KMP算法与后缀自动机跑一遍,然后对后缀自动机做一个拓扑排序,根据拓扑结构更新一遍每个串出现的次数就可以了。然后直接沿着KMP的next数组以及后缀自动机节点的父亲边走就可以了。
代码如下:
#include <stdio.h>
#include <string.h>
const int maxn = ; struct sanode{
sanode *f, *ch[];
int l, m;
sanode(){
f = ;
memset(ch, , sizeof(ch));
l = , m = ;
}
void init(){
f = ;
memset(ch, , sizeof(ch));
l = , m = ;
}
}sam[maxn*], *b[maxn*];
int cnt[maxn], tot;
sanode *tail, *s;
char str[maxn];
int f[maxn]; //KMP
struct resultSet{
int l, m;
}res[maxn]; void addSuffix(int c, int len){
sanode *p = tail, *np = &sam[++tot];
np->init();
tail = np;
np->l = len; for(;p&&!p->ch[c];p=p->f) p->ch[c] = np;
if(!p) np->f = s;
else{
if(p->ch[c]->l == p->l + ) np->f = p->ch[c];
else{
sanode * q = p->ch[c], *r = &sam[++tot];
*r = *q;
r->l = p->l + ;
r->m = ;
q->f = np->f = r;
for(;p && p->ch[c]==q; p=p->f) p->ch[c] = r;
}
}
} void topSortSuffix(int len){
for(int i = ; i <= tot; i ++) cnt[sam[i].l] ++;
for(int i = ; i <= len; i ++) cnt[i] += cnt[i-];
for(int i = ; i <= tot; i ++) b[--cnt[sam[i].l]] = &sam[i]; for(int i = tot; i > ; i --){
b[i]->f->m += b[i]->m;
}
}
void KMP(){
int c = ;
for(int i = ; str[i]; i ++){
while(c && str[c+] != str[i])
c = f[c];
if(str[c+] == str[i])
f[i] = ++c;
}
} int main(){
int len = ;
s = tail = &sam[tot = ];
s->init();
scanf("%s", str+);
for(len = ; str[len]; len ++){
addSuffix(str[len] - 'A', len);
}
len --;
topSortSuffix(len);
KMP();
sanode *p = tail;
int number = ;
for(; len; len = f[len]){
while(p && p->l > len)
p = p->f;
if(!p) break;
if(p->l == len){
res[number].l = len;
res[number].m = p->m;
number ++;
}
}
printf("%d\n", number);
for(int i = number - ; i >= ; i --)
printf("%d %d\n", res[i].l , res[i].m); return ;
}
codeforces 432D Prefixes and Suffixes的更多相关文章
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- Codeforces 432D Prefixes and Suffixes:KMP + dp
题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解 ...
- Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- 432D Prefixes and Suffixes
题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
随机推荐
- sum_series() 求一列数的指定个数的数和(5个数字的和)
#include <stdio.h> #include <stdarg.h> /*用sum_series() 求一列数的指定个数的数和(5个数字的和)*/ double sum ...
- canvas使用注意点
1.canvas中文教程https://developer.mozilla.org/zh-CN/docs/Canvas_tutorial 2.canvas默认宽高是300.150,为避免异常,最好使用 ...
- 【ElasticSearch】
ElasticSearch是基于Lucene开发的分布式搜索框架,包含如下特性: 分布式索引.搜索 索引自动分片.负载均衡 自动发现机器.组建集群 支持Restful 风格接口 配置简单等.
- 映像备份与恢复管理工具Easy Image X使用说明
Easy Image X(简称EIX)是一个支持Ghost映像(.gho)和ImageX映像(.wim)的映像管理工具,具有友好的图形界面,仅需几步简单操作即可完成映像备份与恢复工作.维护时使用最多的 ...
- loadrunner 一个诡异问题
最近使用loadrunner压测一个项目的时候,发现TPS波动巨大.且平均值较低.使用jmeter压测则没有这个问题.经过多方排查发现一个让人极度费解的原因: 原脚本: //脚本其他代码...... ...
- http://www.linuxidc.com/Linux/2015-02/114265.htm
http://www.linuxidc.com/Linux/2015-02/114265.htm
- C++的表驱动法
目的:使用表驱动法,替换复杂的if/else和switch/case语句. 说明:JS 等其他语言也都支持的. 表驱动发示例:http://blog.csdn.net/zhouyulu/article ...
- Linux下使用clock_gettime给程序计时
http://www.cnblogs.com/daqiwancheng/archive/2010/07/01/1769522.html
- SQLite入门与分析(七)---浅谈SQLite的虚拟机
写在前面:虚拟机技术在现在是一个非常热的技术,它的历史也很悠久.最早的虚拟机可追溯到IBM的VM/370,到上个世纪90年代,在计算机程序设计语言领域又出现一件革命性的事情——Java语言的出现,它与 ...
- c++模板注意事项
c++模板类 分类: C++2012-08-20 21:28 7108人阅读 评论(2) 收藏 举报 c++编译器instantiationiostreamlinker编程 c++模板类 分类: 数据 ...