题目链接: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. 4.C语言文件操作

    总论 1.fopen r以只读方式打开文件,该文件必须存在 r+以可读写方式打开文件,文件必须存在 rb+读写打开一个二进制文件,允许读写数据,文件必须存在 rw+读写打开一个文本文件,允许读和写 w ...

  2. MetaSploit攻击实例讲解------工具Meterpreter常用功能介绍(kali linux 2016.2(rolling))(详细)

    不多说,直接上干货! 说在前面的话 注意啦:Meterpreter的命令非常之多,本篇博客下面给出了所有,大家可以去看看.给出了详细的中文 由于篇幅原因,我只使用如下较常用的命令. 这篇博客,利用下面 ...

  3. html页面全屏化显示

    <html><head><script>// toggle full screen function toggleFullScreen() { if (!docum ...

  4. 1355: [Baltic2009]Radio Transmission

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 958  Solved: 659[Submit][Status][Discuss] Description ...

  5. PHP万能的连接数据库

    <?php class DB{ const HOST='127.0.0.1'; const USER='root'; const PASS='root'; const DATA='mooc'; ...

  6. 让IE9以下版本的浏览支持html5,CSS3的插件

    随着html5(后面用h5代表)标签越来越广泛的使用,IE9以下(IE6-IE8)不识别h5标签的问题让人很是烦恼. 在火狐和chrome之类的浏览器中,遇到不认识的标签,只要给个display:bl ...

  7. 鼠标点击textarea后,在光标后追加内容

    $("#insertMsg").on("click",function(){ //获取下拉选项框的值 var textFeildValue = $(" ...

  8. iOS下调用元素的focus方法,input元素不聚焦,键盘不弹起的问题

    页面元素 <input type="text" ref="elInput"/> <div style="margin-top:20p ...

  9. python 爬虫简介

    初识Python爬虫 互联网 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现 ...

  10. ECNUOJ 2613 Gamma 函数

    Gamma 函数 Time Limit:1000MS Memory Limit:65536KBTotal Submit:237 Accepted:138 Description  Pollux最近在复 ...