手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973

D. Prefixes and Suffixes

time limit per test

1:second

memory limit per test:

256 megabytes

input:

standard input

output:

standard output

You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.

Let's introduce several definitions:

  • A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj.
  • The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
  • The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].

Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.

Input

The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) —  string s. The string only consists of uppercase English letters.

Output 

In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers lici. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.

Sample test(s)
input
ABACABA
output
3
1 4
3 2
7 1
input
AAA
output
3
1 3
2 2
3 1
 
 
题目大意 :给一个字符串,求其前缀等于后缀的子串,输出子串的长度和其在原串中出现的次数,子串长度要求曾序输出
 
题目分析 :首先得到母串的next数组,next数组的含义是next[j]的值表示str[0...j-1](我的next[0]是-1)这个子串的前后缀匹配的最长长度,如样例1
index  0  1  2  3  4  5  6  7
str    A  B  A  C  A  B  A
next   -1 0  0  1  0  1  2  3
next[6] = 2即ABACAB这个子串的前后缀最长匹配是2(AB)
由此性质我们可以发现满足条件的子串即是next[next[len。。。]]不断往前递归直到为0,因为长的可能会包含短的,我们可以递归得到re数组(re记录的就是子串出现的次数)re数组的递归式为re[ next[temp] ] += re[temp];
 
 
 #include <cstdio>
#include <cstring>
int const MAX = 1e5 + ;
char str[MAX];
int next[MAX], re[MAX], le[MAX], sub[MAX];;
int len, count; void get_next()
{
int i = , j = -;
next[] = -;
while(str[i] != '\0')
{
if(j == - || str[i] == str[j])
{
j++;
i++;
next[i] = j;
}
else
j = next[j];
}
} void kmp()
{
get_next();
len = strlen(str);
memset(re,,sizeof(re));
for(int i = ; i <= len; i++)
re[i] = ;
int temp = len;
for(; temp > ; temp--)
if(next[temp])
re[ next[temp] ] += re[temp];
count = ;
le[count] = len;
sub[count++] = ;
len = next[len];
while(len)
{
le[count] = len;
sub[count++] = re[len];
len = next[len];
}
} int main()
{
while(scanf("%s", str) != EOF)
{
kmp();
printf("%d\n",count);
for(int i = count - ; i >= ; i--)
printf("%d %d\n",le[i], sub[i]);
}
}

Codeforces 432D Prefixes and Suffixes kmp的更多相关文章

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

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

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

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

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

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

  4. codeforces 432D Prefixes and Suffixes

    由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀.另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了.先分别用KMP算法与后缀自动机跑一遍,然 ...

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

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

  6. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

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

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

  8. 432D Prefixes and Suffixes

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

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

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

随机推荐

  1. python基础一 day14 复习

    迭代器和生成器迭代器:双下方法 : 很少直接调用的方法.一般情况下,是通过其他语法触发的可迭代的 —— 可迭代协议 含有__iter__的方法('__iter__' in dir(数据))可迭代的一定 ...

  2. AEE加密解密

     from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class AesHandler(object):    def ...

  3. java B转换KB MB GB TB PB EB ZB

    public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...

  4. js正则函数match、exec、test、search、replace、split使用集合

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  5. ThinkPHP5.0-多语言切换

    这两天做得项目中需要多语言切换,于是乎就看了看文档,感觉有些乱,就使用了终极必杀--百度. 借鉴了网上各位大佬所集成.整理出一篇比较适合类似我这种比较菜的随笔吧. 请各位大佬轻虐.感谢. 首先,不说其 ...

  6. shell脚本,awk 匹配的做修改后打印,不匹配的打印。

    文件file内容如下a 1a 2b 3b 4 b 5c 6c 7 要求:第一列匹配b时,如果第二列大于3,那么将第二列加上1后打印,其余的原封不动打印.结果如下: a 1a 2b 3b 5 b 6c ...

  7. 看结果,测试?java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 参数传递?

    看结果1? package com.swift; class ArrayString { public static void main(String[] args) { String str = & ...

  8. mysql delete 表无法用别名

    delete from exam_paper_question_opt a WHERE a.OPTION_ID = 65630 and a.QUESTION_ID = 28656 AND a.EXAM ...

  9. 常用c++函数

    strrev(str)  (str为字符串)倒序输出字符串 floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整 ...

  10. 【curl】【php】curl报错,错误代码77,CURLE_SSL_CACERT_BADFILE (77)解决方法

    CURLE_SSL_CACERT_BADFILE (77) - 读取 SSL CA 证书时遇到问题(可能是路径错误或访问权限问题) 在微信接口相关开发时容易出现此问题 这一般是因为服务更新了相关的软件 ...