题目链接:http://codeforces.com/contest/432/problem/D

题意:对一个长度不超过10^5的字符串。按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现的次数。(可重叠)

#include <cstdio>
#include <cstring> const int N=100005; char str[N];
int next[N],cnt[N],ansp[N],ansn[N],n; void getNext (char s[],int len)
{
next[0]=-1;
int i=0,j=-1;
while (i<len)
{
if (j==-1 || s[i]==s[j])
next[++i]=++j;
else j=next[j];
}
} void KMP ()
{
int i=0,j=0;
while (i<n)
if (j == -1 || str[i] == str[j])
i++,j++,cnt[j]++;
else
j=next[j]; for (i=n;i>0;i--) //统计每一个前缀出现次数,cnt[i]表示长度为i的前缀出现了cnt[i]次
if (next[i] != -1)
cnt[next[i]] += cnt[i];
} int main ()
{
scanf("%s",str);
n=strlen(str);
memset(cnt,0,sizeof(cnt));
getNext(str,n);
KMP();
int t=n,sum=0;
while (t) //前缀匹配后缀
{
ansp[sum]=t;
ansn[sum++]=cnt[t]; //
t=next[t];
}
printf("%d\n",sum);
for (int i=sum-1;i>=0;i--)
printf("%d %d\n",ansp[i],ansn[i]);
return 0;
}

CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)的更多相关文章

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

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

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

  3. codeforces432D Prefixes and Suffixes(kmp+dp)

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

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

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

  5. Codeforces 432 D. Prefixes and Suffixes

    用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...

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

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

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

  8. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  9. Codeforces 432D Prefixes and Suffixes kmp

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

随机推荐

  1. 【转】webshell检测——使用auditd进行system调用审计

    本文档将介绍:如何通过Linux审计系统auditd监测WebShell执行系统命令的行为. 测试环境:CentOS7.0_x64 auditd简介 Linux审计系统提供了一种跟踪系统上与安全相关的 ...

  2. [转]Python UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法

    UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法 python在安装时,默认的编码是ascii,当 ...

  3. git的学习笔记整理

    Git学习较好的网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001373 ...

  4. Struts2中Struts.xml的作用

    struts.xml 为Struts 2的核心配置文件.struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.struts.xml中主要配置Stru ...

  5. rest_framework_HyperlinkedIdentityField

    #生成链接 HyperlinkedIdentityField class UserInfoSerializer(serializers.ModelSerializer): group = serial ...

  6. POJ 3671 DP or 乱搞

    思路: 1.DP f[i][j]:前i个数 最后一个数是j的最小花费 f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j));1<=k<=j 这种做法比较有普遍性 ...

  7. vuejs helloworld

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. MAC下搭建appium UI自动化环境

    参考资料: http://qa.blog.163.com/blog/static/190147002201510161119832/ http://blog.csdn.net/liuchunming0 ...

  9. [NOI2015]品酒大会(SA数组)

    [NOI2015]品酒大会 题目描述 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战 两个环节,分别向优胜者颁发"首席品酒家"和" ...

  10. Android Studio获取开发版SHA1值和发布版SHA1值,详细过程

    转自原文 Android Studio获取开发版SHA1值和发布版SHA1值的史上最详细方法 前言: 今天我想把百度地图的定位集成到项目中来,想写个小小的案例,实现一下,但在集成百度地图时首先要申请秘 ...