手动转田神的大作: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. 获取显示设备的名称及PNPDeviceID

    实现效果: 知识运用: ManagementObjectSearcher类和ManagementObject类 实现代码: private void button1_Click(object send ...

  2. kitti raw data development kit的使用

    run_demoVelodyne.m使用:http://blog.csdn.net/qq_33801763/article/details/78959205   https://www.cnblogs ...

  3. 制作新的train,test数据集

    之前的数据集的train和test是直接按照网上下载的数据的前7000个作为训练集,后2212个作为测试集.看得出来,这个数据集是由开车录制视频转换来的图片数据,后面2000多个图片的场景和前面的场景 ...

  4. CPP-基础:C++拷贝构造函数详解

    一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: ; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象 ...

  5. javase(4)_数组

    一.数组概述 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任意类型,包括基本类型和 ...

  6. 【OS_Linux】Linux系统中目录及文件管理

    1.Linux系统中目录的树状结构 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录, ...

  7. Java开发工具下载

    一.Tomcat下载: http://tomcat.apache.org/ 二.Maven下载: http://maven.apache.org/download.cgi 三.eclipse下载: h ...

  8. (60)zabbix网络发现介绍Network Discovery

    网络发现简介 网络发现有什么用?网络发现怎么配置? 我们带着这两个问题开始我们的网络发现之旅. 比如小明有100台服务器,不想一台台主机去添加,能不能让zabbix自动添加主机呢,当然可以,网络发现便 ...

  9. Linux下基于LVM调整分区容量大小的方法

    Linux下调整分区容量大小的方法(适用于centos6-7) 说明:以下方法均使用centos6.9和centos7.4进行测试. Centos6分区容量调整方法 1.web分区空间不足,新添加一块 ...

  10. hashlib模块,hmac模块

    6.11自我总结 1.hashlib模块(文件传输中将传输内容用指定算法进行处理) hash是一种算法(Python3.版本里使用hashlib模块代替了md5模块和sha模块,主要提供 SHA1.S ...