大意:给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n。求出所有的长度n

例如

‘alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}

分析:考察对于next数组的理解

next数组表示i之前的k个字符与该串钱k个字符匹配

所以

next[l] 就表示最大后缀满足与前缀相同的最大字串

然后  在对获得的字串进行同样的求解  最终得到结果

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 400005;
 7
 8 int next[maxn];
 9 int ans[maxn];
10
11 void get(char *s) {
12     int l = strlen(s);
13     int j = 0, k = -1;
14     next[0] = -1;
15     while(j < l) {
16         if(k == -1 || s[j] == s[k]) {
17             next[++j] = ++k;
18         } else {
19             k = next[k];
20         }
21     }
22 }
23 char s[maxn];
24
25 int main() {
26     while(EOF != scanf("%s",s)) {
27         get(s);
28         int tot = 0;
29         int k = strlen(s);
30         while(k) {
31             ans[tot++] = k;
32             k = next[k];
33         }
34         for(int i = tot - 1; i >= 0; i--) {
35             printf(i == tot - 1 ? "%d" : " %d", ans[i]);
36         } puts("");
37     }
38 }

View Code

转载于:https://www.cnblogs.com/zhanzhao/p/4761660.html

poj2752Seek the Name, Seek the Fame【kmp next数组应用】相关推荐

  1. Seek the Name, Seek the Fame(KMP-next数组详解)

    Seek the Name, Seek the Fame 目录 Seek the Name, Seek the Fame 解题过程 举个例子 AC代码 The little cat is so fam ...

  2. POJ2752-Seek the Name, Seek the Fame【KMP】

    正题 题目链接: http://poj.org/problem?id=2752 大意 一个字符串,求所有的前缀等于后缀的长度 解题思路 用KMP求出Next数组.然后最大的那个肯定是长度,然后让j=l ...

  3. Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

    题意:给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀. 输出所有前缀的结束位置. 就是求前缀和后缀相同的那个子串的长度  然后从小到大输出,主要利用next数组求解. 例如 &qu ...

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

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

  5. KMP Seek the Name,Seek the Fame

    Seek the Name,Seek the Fame 时间限制: 1 Sec 内存限制: 128 MB 题目描述 给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度. 字符串长度不超过四 ...

  6. 【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)

    题干: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked t ...

  7. Seek the Name, Seek the Fame POJ - 2752(KMP应用)

    Stringland有一只小猫非常有名,许多夫妇跋涉到Stringland请小猫为他们的新出生的婴儿起名字,他们寻求这个名字,同时寻求名声.为了摆脱这种无聊的工作,创新的小猫制定了一个简单而奇妙的算法 ...

  8. 【hash】Seek the Name, Seek the Fame

    [哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...

  9. poj-2752 Seek the Name, Seek the Fame **

    /* * KMP **/ #include <cstdio>#include <cstring>using namespace std; const int maxL = 40 ...

最新文章

  1. 洛谷P2380 狗哥采矿
  2. Tomexam在线考试系统 2.1
  3. 单击触发jquery.autocomplete的两种方法
  4. linux 更改文件和目录权限
  5. 【转】QT中添加的资源文件qrc时的路径问题小结
  6. 【分享】C# 字节帮助类 ByteHelper
  7. .NET 云原生架构师训练营(模块二 基础巩固 日志)--学习笔记
  8. oracle挂载到空闲进程,Oracle部分后台进程
  9. SAP License:SAP自学SAP常见的问题二
  10. Linux系统启动过程,grub重装。
  11. 湖北省首条短途运输航线“荆门—武汉”航线运营
  12. GNU make 汇总
  13. jms与ActivityMQ中的简单使用
  14. Intellij IDEA如何打包jar文件?
  15. linux bridge 添加fdb,Linux协议栈--网桥设备的实现
  16. Jupyter启动报错 ImportError: DLL load failed while importing error
  17. 配置 nginx server 出现nginx: [emerg] root directive is duplicate in /etc/nginx/server/blogs.conf:107...
  18. 软件环境 硬件环境java,软件环境和硬件环境都指什么?
  19. 【ArcGIS错误异常100问】之002:Error 000735 简化容差:值是必需的(简化线、简化面工具)
  20. ObjectARX中反应器的使用

热门文章

  1. 世界上最狠最毒的动物是什么?
  2. 职场中,为什么有人经常说:做得越多,死得越快?
  3. 湿气重的人,脸上会有哪些信号?
  4. 狐假虎威的故事,狐假虎威的故事告诉我们什么道理?
  5. 你确定了项目,就不要乱变了
  6. ES6函数参数的解构赋值,以及2种设置参数默认值方法的区别
  7. 死锁的处理策略——预防死锁
  8. Spring_day1
  9. ssis修改数据库数据_SSIS平衡数据分配器概述
  10. sql azure 语法_如何将内部SQL Server数据库迁移到Azure