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

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

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. const int N=100005;
  5.  
  6. char str[N];
  7. int next[N],cnt[N],ansp[N],ansn[N],n;
  8.  
  9. void getNext (char s[],int len)
  10. {
  11. next[0]=-1;
  12. int i=0,j=-1;
  13. while (i<len)
  14. {
  15. if (j==-1 || s[i]==s[j])
  16. next[++i]=++j;
  17. else j=next[j];
  18. }
  19. }
  20.  
  21. void KMP ()
  22. {
  23. int i=0,j=0;
  24. while (i<n)
  25. if (j == -1 || str[i] == str[j])
  26. i++,j++,cnt[j]++;
  27. else
  28. j=next[j];
  29.  
  30. for (i=n;i>0;i--) //统计每一个前缀出现次数,cnt[i]表示长度为i的前缀出现了cnt[i]次
  31. if (next[i] != -1)
  32. cnt[next[i]] += cnt[i];
  33. }
  34.  
  35. int main ()
  36. {
  37. scanf("%s",str);
  38. n=strlen(str);
  39. memset(cnt,0,sizeof(cnt));
  40. getNext(str,n);
  41. KMP();
  42. int t=n,sum=0;
  43. while (t) //前缀匹配后缀
  44. {
  45. ansp[sum]=t;
  46. ansn[sum++]=cnt[t]; //
  47. t=next[t];
  48. }
  49. printf("%d\n",sum);
  50. for (int i=sum-1;i>=0;i--)
  51. printf("%d %d\n",ansp[i],ansn[i]);
  52. return 0;
  53. }

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. 16.允许重复的multimap

    #include <iostream> #include <map> #include <cstring> using namespace std; void ma ...

  2. android页面布局(listview填充中间)

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  3. 使用Chrome浏览器,让我们远离(所有)广告

    你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...

  4. 日志文件支持unicode字符的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 开发的程序兼容多字节字符集和unicode字符集,最近发现一个问题,在unicode字符集下输出的日志文件是乱码的.显 ...

  5. SQL Server在用户自定义函数(UDF)中使用临时表

    SQL Server在用户自定义函数中UDF使用临时表,这是不允许的. 有时是为了某些特殊的场景, 我们可以这样的实现: CREATE TABLE #temp (id INT) GO INSERT I ...

  6. Slimming Paint (a.k.a. Redesigning Painting and Compositing)

    Slimming Paint is a Paint team project to re-implement the Blink<->cc picture recording API to ...

  7. Vue总结(二)

    原始引用:开发时使用开发版本,线上使用生产版本. 原始引用到html中,在浏览器中控制台输入Vue,输出一个函数就可以. defineProperties实现的数据绑定. //defineProper ...

  8. vue中使用console.log无效

    webpack开发环境下,在vue中使用console.log无效,一直以为webpack出了问题. 使用window.console.log()就能够顺利在浏览器控制台输出了. 以及 在axios请 ...

  9. numpy基础篇-简单入门教程2

    import numpy as np Array 数组 print(np.zeros((2, 2))) # [[0. 0.] [0. 0.]] print(np.ones((2, 2))) # [[1 ...

  10. *Mapper.xml文件头

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...