Codeforces 432 D. Prefixes and Suffixes
用扩展KMP做简单省力.....
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 li ci.
Numbers li ci mean
that the prefix of the length li matches
the suffix of length li and
occurs in string s as a substringci 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=100100; char T[maxn],P[maxn];
int next[maxn],ex[maxn]; void pre_exkmp(char P[])
{
int m=strlen(P);
next[0]=m;
int j=0,k=1;
while(j+1<m&&P[j]==P[j+1]) j++;
next[1]=j;
for(int i=2;i<m;i++)
{
int p=next[k]+k-1;
int L=next[i-k];
if(i+L<p+1) next[i]=L;
else
{
j=max(0,p-i+1);
while(i+j<m&&P[i+j]==P[j]) j++;
next[i]=j; k=i;
}
}
} void exkmp(char P[],char T[])
{
int m=strlen(P),n=strlen(T);
pre_exkmp(P);
int j=0,k=0;
while(j<n&&j<m&&P[j]==T[j]) j++;
ex[0]=j;
for(int i=1;i<n;i++)
{
int p=ex[k]+k-1;
int L=next[i-k];
if(i+L<p+1) ex[i]=L;
else
{
j=max(0,p-i+1);
while(i+j<n&&j<m&&T[i+j]==P[j]) j++;
ex[i]=j; k=i;
}
}
} int pos[maxn],sum[maxn],mx=-1; struct ANS
{
int a,b;
}ans[maxn];
int na=0; bool cmp(ANS x,ANS y)
{
if(x.a!=y.a)return x.a<y.a;
return x.b<y.b;
} int lisan[maxn],nl; int main()
{
cin>>P;
pre_exkmp(P);
int n=strlen(P);
for(int i=0;i<n;i++)
{
pos[next[i]]++;
lisan[nl++]=next[i];
mx=max(mx,next[i]);
}
sort(lisan,lisan+nl);
int t=unique(lisan,lisan+nl)-lisan;
for(int i=t-1;i>=0;i--)
{
sum[lisan[i]]=sum[lisan[i+1]]+pos[lisan[i]];
}
for(int i=0;i<n;i++)
{
if(next[i]==n-i)
{
ans[na++]=(ANS){next[i],sum[next[i]]};
}
}
sort(ans,ans+na,cmp);
printf("%d\n",na);
for(int i=0;i<na;i++)
{
printf("%d %d\n",ans[i].a,ans[i].b);
}
return 0;
}
Codeforces 432 D. Prefixes and Suffixes的更多相关文章
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- 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 ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- CF432D Prefixes and Suffixes
CF432D Prefixes and Suffixes 题意 给你一个长度为n的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
随机推荐
- hdu 4715
#include<stdio.h> #include<string.h> int prime[1100000],p[1000000],ans; void pri() { ...
- 安装Apache Felix OSGI Framework小记
Felix是apache的开源OSGI服务框架,到 http://felix.apache.org/downloads.cgi 可以下载到最新的版本. 解压后目录结构如下: felix-framewo ...
- STM32学习笔记2-系统时钟知识及程序配置
一:基本知识 1. STM32F103ZE有5个时钟源:HSI.HSE.LSI.LSE.PLL. ①.HSI是快速内部时钟,RC振荡器,频率为8MHz,精度不高. ②.HSE是快速外部时钟, ...
- 一步步学习Linux开发环境搭建与使用
00.Linux开发环境搭建与使用1--Linux简史 01.Linux开发环境搭建与使用2--Linux系统(ubuntu)安装方案 02.Linux开发环境搭建与使用3--通过虚拟机安装系统(ub ...
- 【Windows Phone设计与用户体验】关于移动产品的Loading用户体验的思考
作为一款运行在移动端上的产品,必定会有一些耗时的操作.为了具有良好的用户体验,Loading效果是必不可少的,而什么形式的Loading才会有良好的用户体验? Loading形式简单分为两类: 一.遮 ...
- oracle数据库单个数据文件的大小限制
之前没有仔细想过这个问题,因为总是不会用到,也没有犯过类似错误. 顺便提一下学习方法吧. 卤肉的学习方法是:常用知识点,熟悉理论并反复做实验,深入理解:不常用的知识点,相关内容都了解大概,遇到问题时想 ...
- operation is executing and cannot be enqueued
http://d2100.com/questions/29022 作为依赖关系的另一个 NSOperation 添加时不调用 NSOperation dealloc 使用文书我看到很多我自定义的 NS ...
- Django学习之manage.py使用
1.django-admin.py startproject mysite 开始一个项目,会初始化一些项目的结构文件 2.python manage.py runserver ip:port 如: p ...
- 解决NGINX的WORDPRESS伪静态规则失效的问题
解决NGINX的WORDPRESS伪静态规则失效的问题 前两天搬到了EMSVPS的PR线路上,用上了最新的WDCP2.0管理面板,支持多用户管理(我们几个合租的VPS,最需要这个功能了),感觉很不错, ...
- R语言与数据分析之九:时间内序列--HoltWinters指数平滑法
今天继续就指数平滑法中最复杂的一种时间序列:有增长或者减少趋势而且存在季节性波动的时间序列的预測算法即Holt-Winters和大家分享.这样的序列能够被分解为水平趋势部分.季节波动部分,因此这两个因 ...