用扩展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. Swift - 自定义函数规则说明

    1,无返回值的函数 1 2 3 func test(name:String){   } 2,返回一个返回值 1 2 3 func test(name:String) -> Bool{     r ...

  2. JavaScript面向对象编程(10)高速构建继承关系之对象拷贝

    前面的样例我们是通过构造器创建对象.而且希望该对象继承来自另外一个构造器的对象 我们也能够直接面向一个对象来达成继承的目的.使用下属步骤: 1.拷贝一个对象 2.给新对象加入属性 /** * 通过拷贝 ...

  3. http staus汇总

    参考http://www.cnblogs.com/cxd4321/archive/2008/11/20/1337776.html 常见HTTP状态码 200 OK 301 Moved Permanen ...

  4. 14.3.4 Phantom Rows 幻影行

    14.3.4 Phantom Rows 幻影行 所谓的幻读为发生在一个事务 当相同的查询产生不同的结果集在不同的时间. 比如,如果一个SELECT被执行2次, 但是第2次返回的记录不是第一次返回的记录 ...

  5. oracle数据库单个数据文件的大小限制

    之前没有仔细想过这个问题,因为总是不会用到,也没有犯过类似错误. 顺便提一下学习方法吧. 卤肉的学习方法是:常用知识点,熟悉理论并反复做实验,深入理解:不常用的知识点,相关内容都了解大概,遇到问题时想 ...

  6. Scriptcase价格调整(五折销售)

    芬兰诺基亚!芬兰诺基亚!最大手机公司倒闭了!王八蛋老板埃洛普,吃喝嫖赌欠下了3.5个亿,带着他的小姨子跑了!我们没有办法,拿着手机抵工资!原价都是一千多.两千多.三千多的手机,统统二百块!统统二百块! ...

  7. 浅谈 Python 程序和 C 程序的整合

    源地址:http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/ 概览 Python 是一种用于快速开发软件的编程语言,它的语法比较简单, ...

  8. hdu 1029(hash)

    传送门:Ignatius and the Princess IV 题意:给n个数,找出出现次数大于等于(n+1)/2的那个数. 分析:大水题,排个序输出中间那个即可,这里随便写个HASHMAP找出次数 ...

  9. Fitnesse使用系列二

    决策表 Fitnesse中提供了好几种表格样式,前面说了.表格是运行測试的关键.从字面看.表格描写叙述的是測试用例.从运行角度看,表格为后端的代码(fitnesse里称作fixture)提供了包名.类 ...

  10. Android经常使用开源组件汇总

    http://www.cnblogs.com/scige/p/3456790.html UI相关 图片 Android-Universal-Image-Loader:com.nostra13.univ ...