由于包含了前缀与后缀,很容易想到用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. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

  2. This transaction has been rolled back, rather than only the current.

    今天上午,收到运维组同事反映某应用系统的其中一个功能报错,不是偶然性事件,每个使用该功能的用户都报错.报错内容为:This transaction has been rolled back, rath ...

  3. poj 3250 Bad Hair Day (单调栈)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. C3p0的参数设置

    C3p0的参数设置:ComboPooledDataSource和BasicDataSource一样提供了一个用于关闭数据源的close()方法,这样我们就可以保证Spring容器关闭时数据源能够成功释 ...

  5. sizeof()函数求各类型变量所占空间的方法

    #include "stdafx.h" #include <iostream> using namespace std; ]) { cout<<sizeof ...

  6. csuoj 1353: Guessing the Number

    这个题我想到要用kmp找到循环节: 但是后面的我就不会做了: 看到题解才知道是字符串的最小表示: #include<cstdio> #include<cstring> #inc ...

  7. Java Development Kit (JDK) 发展历程 及新特性

    SE(J2SE),standard edition,标准版,是我们通常用的一个版本,从JDK 5.0开始,改名为Java SE. EE(J2EE),enterprise edition,企业版,使用这 ...

  8. php nl2br() 函数

    nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />).

  9. altium6.x中自动删除重复走线的位置

    在protel 2004 DXP中,“自动删除走线”的位置就在"PCB Editor"的默认页面,非常好找. 但是升级到了altium 6.7,6.9之后,很多人就找不到这个了. ...

  10. WCF - Windows Service Hosting

    WCF - Windows Service Hosting The operation of Windows service hosting is a simple one. Given below ...