题意:给一个字符串s,问s的某个前缀与后缀相同的情况时,长度是多少。

此题使用KMP的next数组解决。

next数组中,j=next[i],next[i]表示S[0...i-1]的某个后缀(字符串S[i-j,i-1])与字符串S[0...j-1]完全相同,此时的j即该段字符串的长度。

字符串s本身是最长的串,next[L]对应的S[0...next[L]]是次长的符合条件的串,其他更短的串怎么求呢。

由于字符串S[L-j...L-1]等于S[0...j-1],这样在S[0...j-1]时继续利用next[j](令k=next[j])找到S[0...k-1]与S[0...j-1]的某个后缀(字符串S[j-k,j-1])相同,这段字符串等于S[L-k,L-1](即原字符串的某个后缀),此时的k即该段字符串的长度。如此下去,直到next[]数值为0,可以找到所有的符合条件的字符串。

这个过程可以使用递归倒序输出。

 #include <iostream>
 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 #define MAXN 400005
 using namespace std;
 int  next[MAXN];
 char str[MAXN];
 void getFail()
 {
     ; str[i]; ++i)
     {
         int j=next[i];
         while(j&&str[i]!=str[j]) j=next[j];
         next[i+]=(str[i]==str[j])?j+:;
     }
 }
 void show(int p)
 {
     if(!next[p]) return ;
     show(next[p]);
     printf("%d ",next[p]);
 }
 int main()
 {
     while(gets(str))
     {
         getFail();
         int L=strlen(str);
         show(L);
         printf("%d\n",L);
     }
     ;
 }

POJ 2752 - Seek the Name, Seek the Fame (KMP)的更多相关文章

  1. poj-------------(2752)Seek the Name, Seek the Fame(kmp)

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

  2. Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (J ...

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

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

  5. POJ 2751:Seek the Name, Seek the Fame(Hash)

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

  6. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  7. 题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  8. POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...

  9. POJ 2752 Seek the Name, Seek the Fame (KMP)

    传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...

随机推荐

  1. WebSocket 浅析

    版权声明:本文由史燕飞原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/241 来源:腾云阁 https://www.qclo ...

  2. js获取多个标签元素的内容,并根据元素的内容修改标签的属性

    <html > <head> <title>无标题文档</title> </head> <body> <div class ...

  3. Event 讲解

    应用场景:某件事发生时,需要采取多步的动作,此时就用到了 使用方法:创建event方法一,使用命令 make:event  生成事件在app/Events目录下,命令make:listener 生成监 ...

  4. NodeJS 各websocket框架性能分析

    For a current project at WhoScored, I needed to learn JavaScript, Node.js and WebSocket channel, aft ...

  5. 190. Reverse Bits -- 按位反转

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  6. webService—使用javaxws发布自己的webService

    1.创建一个普通类,使用javaxws注解标示该类 2.解析wsdl文档生成类后调用webService

  7. 给Eclipse中hibernate.cfg.xml配置文件加提示

    在hibernate框架需要的jar包中找到hibernate3.jar,并用压缩软件打开,如图: 2 选择org文件夹--打开下一级文件夹 3 点击类型,方便找到dtd文件,下拉查看dtd文件,有两 ...

  8. Notepad++ 配置java编译环境

    仅限于学习java或小的java程序使用.正常写代码还是eclipse吧 ---------------------分割线----------------------------- 1.配置JDK环境 ...

  9. 克隆机器后eth1变为eth0问题

    1. 清空该文件 2.进入网络配置文件把HADDR 和UUID注释掉,并重启 3.成功修改eth0 4. 4.可以结合这篇帖子来看   http://www.cnblogs.com/zydev/p/4 ...

  10. 伪命题:PHP识别url重写请求

    手上有一个网站,然后启用了伪静态,因为一些设置上的原因,一段时间后,发现收录的都是.php的文件,而启用的伪静态地址则收录很少,在更改设置后,想尽快去掉.php的收录,然后想将.php的地址转向.ht ...