用扩展KMP做简单省力.....

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 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.

Sample test(s)
input
ABACABA
output
3
1 4
3 2
7 1
input
AAA
output
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的更多相关文章

  1. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

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

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

  3. 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 ...

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

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

  5. 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 ...

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

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

  7. codeforces432D Prefixes and Suffixes(kmp+dp)

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

  8. CF432D Prefixes and Suffixes

    CF432D Prefixes and Suffixes 题意 给你一个长度为n的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...

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

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

随机推荐

  1. 全民Scheme(0):lat的定义

    接下来我会写一写Scheme的学习笔记.嗯,Scheme是属于小众的语言,但合适用来教学的. 什么是lat,就是遍历list里的每一个S-expression,假设发现当中某个不是atom的,则返回f ...

  2. 2013Esri全球用户大会之ArcGIS for Desktop

    Q1:ArcGIS 10.2 for Desktop中有哪些新特性?     增强的质量和性能        扩展并行处理能力        许多软件质量的改进        优化的文件处理     ...

  3. Android面向HTTP协议发送get请求

    /** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...

  4. 玩转Windows服务系列——创建Windows服务

    原文:玩转Windows服务系列——创建Windows服务 创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Wi ...

  5. [Cocos2d-x]节点之间的相互通讯

    在做.NET开发时,对象之间的相互通讯一般使用事件(event)实现,事件概念是.NET对Delegate的封装. 在Cocos2d-x开发过程中,对象之间的通讯刚开始时不知道如何实现,于是想到c++ ...

  6. Uva - 11419 - SAM I AM

    题意:一个矩形——R*C的网格,在某些位置上有石头,在网格外开一炮可以打掉该行或者该列的石头,求打掉这些石头最少需要多少门大炮,位置分别设在哪行哪列(0<R<1001, 0 < C ...

  7. 离别&#183;伤

    天边露出尖尖的小月  青涩似梦  一点萤火虫落在时光的蘋  搜索  若然恍惚  莺归晚巢  日隐西山  至此予你别过  未曾听你轻启朱唇  未曾见你合身回眸  风,走过紫罗兰花  淡淡的香绕过你的长发 ...

  8. 遍历指定包名下所有的类(支持jar)(转)

    支持包名下的子包名遍历,并使用Annotation(内注)来过滤一些不必要的内部类,提高命中精度. 通过Thread.currentThread().getContextClassLoader()获取 ...

  9. 绝杀600元以下智能手机的夏新小V二代-专栏-速途网

    绝杀600元以下智能手机的夏新小V二代-专栏-速途网 绝杀600元以下智能手机的夏新小V二代

  10. Application to find the maximum temperature in the weather dataset

    import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop. ...