题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度。

解法:利用KMP中next[ ]数组的性质,依次找到前缀、后缀匹配的字符串。

  1. 1 #include<cstdio>
  2. 2 #include<cstdlib>
  3. 3 #include<cstring>
  4. 4 #include<iostream>
  5. 5 using namespace std;
  6. 6
  7. 7 const int N=400010;
  8. 8 int next[N],a[N];
  9. 9 int n;
  10. 10 char s[N];
  11. 11
  12. 12 void kmp()
  13. 13 {
  14. 14 memset(next,0,sizeof(next));
  15. 15 int p=0;
  16. 16 next[1]=0;
  17. 17 for (int i=2;i<=n;i++)
  18. 18 {
  19. 19 while (s[i]!=s[p+1] && p) p=next[p];
  20. 20 if (s[i]==s[p+1]) p++;
  21. 21 next[i]=p;
  22. 22 }
  23. 23 }
  24. 24 int main()
  25. 25 {
  26. 26 while (scanf("%s",s+1)!=EOF)
  27. 27 {
  28. 28 n=strlen(s+1);
  29. 29 kmp();
  30. 30 int t=0;
  31. 31 for (int i=n;i;i=next[i])
  32. 32 a[++t]=i;
  33. 33 for (int i=t;i>=1;i--)
  34. 34 printf("%d ",a[i]);
  35. 35 printf("\n");
  36. 36 }
  37. 37 return 0;
  38. 38 }

【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)的更多相关文章

  1. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  2. (KMP)Seek the Name, Seek the Fame -- poj --2752

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  3. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  4. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  5. poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10204   Ac ...

  6. poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14106   Ac ...

  7. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

    Seek the Name, Seek the Fame Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 24000 ...

  8. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  9. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13840   Ac ...

  10. POJ 2752:Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...

随机推荐

  1. 【JavaWeb】书城项目

    书城网站 项目说明 项目地址 阶段一 登录.注册的验证 使用 jQuery 技术对登录中的用户名.密码进行非空验证: 使用 jQuery 技术和正则表达式对注册中的用户名.密码.确认密码.邮箱进行格式 ...

  2. Java通过基姆拉尔森公式判断当前日期是不是工作日

    基姆拉尔森公式 算法如下: 基姆拉尔森计算公式 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 在公式中d表示日期中的日数,m表示月份数,y表示年数. 注意:在公 ...

  3. Tomcat-8.5.23 基于域名和端口的虚拟主机

    下载tomcat yum install java -y cd /opt/ wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.23/b ...

  4. ctfhub技能树—sql注入—报错注入

    打开靶机 payload 1 Union select count(*),concat((查询语句),0x26,floor(rand(0)*2))x from information_schema.c ...

  5. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp

    [root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf root@192.168.30.23:/etc/sysctl.d/k8s.conf@@@@@@@@@@@ ...

  6. oracle rac搭建单实例DG步骤(阅读全篇后再做)

    环境介绍 主库: 主机名 rac01 rac02 实体IP 10.206.132.232 10.206.132.233 私有IP 192.168.56.12 192.168.56.13 虚拟IP 10 ...

  7. Core3.1 微信v3 JSAPI支付

    1.前言 "小魏呀,这个微信支付还要多久?","快了快了老板,就等着最后一步了...","搞快点哈,就等着上线呢","...... ...

  8. es_python_操作

    获取es索引 https://www.itranslater.com/qa/details/2583886977221264384

  9. 01. struts2介绍

    struts2优点 与Servlet API 耦合性低.无侵入式设计 提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能 支持多种表现层技术,如:JSP.freeMarker.veloc ...

  10. 使用remix实现给合约账户转账

    实现内容:从remix上的虚拟账户上转账给自己编写的智能合约账户 前提基础:对solidity有一些基础了解,对以太坊的账户机制有一定了解. 账户 在以太坊中账户的唯一标识是地址(address). ...