手动转田神的大作: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]);
}
}

最新文章

  1. View的drawRect方法
  2. C 排序法
  3. Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务模块的交互
  4. 【转载】计算机视觉(CV)前沿国际国内期刊与会议
  5. UVa11584 - Partitioning by Palindromes(区间DP)
  6. 在C#里实现各种窗口切换特效,多达13种特效
  7. VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
  8. poj 3686 The Windy&#39;s
  9. scrapy跟pyspider的杂谈
  10. 使用Quartz 2D擦除图片
  11. Day 21 内存处理与正则
  12. 2016蓝桥杯&quot;取球博弈&quot;问题
  13. loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
  14. pip更换国内源
  15. u-boot移植(十二)---代码修改---支持DM9000网卡
  16. NATS—协议详解(nats-protocol)
  17. SpringCloud 启动时报No active profile set, falling back to default profiles default
  18. hashlib 简单的登录例子
  19. 使用AutoMapper实现Dto和Model的自由转换(中)
  20. ES6必知必会 (三)—— 数组和对象的拓展

热门文章

  1. 使用Google Colab训练神经网络(二)
  2. java script DOM BOM
  3. Bootstrap历练实例:验证状态
  4. shell脚本,如何监控mysql数据库。
  5. 【简●解】 LG P2730 【魔板 Magic Squares】
  6. NOIP 2018数据点
  7. [LUOGU] P1551 亲戚
  8. Linux基础学习-使用Squid部署代理缓存服务
  9. Python9-继承2-day25(大年初二)
  10. Python9-装饰器进阶-day12