由于包含了前缀与后缀,很容易想到用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的更多相关文章

  1. Codeforces 432D Prefixes and Suffixes(KMP+dp)

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

  2. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

  3. codeforces - 432D Prefixes and Suffixes (next数组)

    http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...

  4. Codeforces 432D Prefixes and Suffixes:KMP + dp

    题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解 ...

  5. Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)

    题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...

  6. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  7. 432D Prefixes and Suffixes

    题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...

  8. 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 ...

  9. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...

随机推荐

  1. Vijos P1061 迎春舞会之三人组舞 DP

    题目链接:https://vijos.org/p/1061 n个人选出3*m人,排成m组,每组3人. 站的队形——较矮的2个人站两侧,最高的站中间. 从对称学角度来欣赏,左右两个人的身高越接近,则这一 ...

  2. spring mvc处理流程概述

    大部分Java应用都是Web应用,展现层是Web应用不可忽略的重要环节.Spring为展现层提供了一个优秀的Web框架-Spring MVC.和众多其他Web框架一样,它基于MVC设计理念,此外,它采 ...

  3. appcache checking update

    <!DOCTYPE html> <html manifest="a.appcache"> <head> <title></ti ...

  4. JAVA入门第一季(mooc-笔记)

    笔记相关信息 /** * @subject <学习与创业>作业1 * @author 信管1142班 201411671210 赖俊杰 * @className <JAVA入门第一季 ...

  5. [转载]常用Web Service汇总(天气预报、时刻表等)

    下面总结了一些常用的Web Service,是平时乱逛时收集的,希望对大家有用. ============================================ 天气预报Web Servic ...

  6. AOT

    预 (AOT) 编译器 https://angular.cn/docs/ts/latest/cookbook/aot-compiler.html To run your app in AoT mode ...

  7. SQLServer数据类型与C#类型对照表

    这是我在开发多层体系结构时遇到的问题,由于VS.NET尚不支持各种数据类型的空值即null, 放入实体类中在各个层之间作为参数传递,所以经查询参考SQL Server 2000的联机丛书和.NET的M ...

  8. 练习--LINUX进程间通信之无名管道PIPE

    IBM上放的这个系统不错,刚好可以系统回温一下LINUX的系统知识. http://www.ibm.com/developerworks/cn/linux/l-ipc/part1/ 感觉年纪大了,前几 ...

  9. PHP reset() 函数

    定义和用法 reset()函数把数组的内部指针指向第一个元素,并返回这个元素的值.若失败,则返回 FALSE. reset()函数用来将数组指针设置回数组的开始位置.如果需要在一个脚本中多次查看或处理 ...

  10. 133. Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...