Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973
D. Prefixes and Suffixes
1:second
256 megabytes
standard input
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.
The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.
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.
ABACABA
3
1 4
3 2
7 1
AAA
3
1 3
2 2
3 1
#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]);
}
}
最新文章
- View的drawRect方法
- C 排序法
- Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务模块的交互
- 【转载】计算机视觉(CV)前沿国际国内期刊与会议
- UVa11584 - Partitioning by Palindromes(区间DP)
- 在C#里实现各种窗口切换特效,多达13种特效
- VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
- poj 3686 The Windy&#39;s
- scrapy跟pyspider的杂谈
- 使用Quartz 2D擦除图片
- Day 21 内存处理与正则
- 2016蓝桥杯";取球博弈";问题
- loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
- pip更换国内源
- u-boot移植(十二)---代码修改---支持DM9000网卡
- NATS—协议详解(nats-protocol)
- SpringCloud 启动时报No active profile set, falling back to default profiles default
- hashlib 简单的登录例子
- 使用AutoMapper实现Dto和Model的自由转换(中)
- ES6必知必会 (三)—— 数组和对象的拓展