题意:给一个字符串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. xocde 静态类库 相对路径 与 绝对路径

    xocde 静态类库 相对路径 与 绝对路径 导入别人的 静态类库,通常我直接用鼠标把文件夹拉到了 xcode的项目里面: 这时,导入的静态类库路径变成了,绝对路径:如果你的项目,不换位置那么编译运行 ...

  2. b.BIO连接器整体框图

    上一讲讲解过NIO的框图,可以看来,NIO通道是目前Tomcat7以后的默认的通道的推荐配置,在Tomcat6和以前的配置中,BIO是主流的配置: 只需要修改protocol协议部分即可,而后续还有A ...

  3. 清理无用的CSS样式的几个工具(转)

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  4. 字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  5. db:seed 更好的生成测试数据

    make:model -m -> 在database/migrations/目录下生成的table表中设置表的字段名和字段类型->在app/目录下对应的模型文件中设置可添加字段 -> ...

  6. CFX客户端调用报错

    Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: unex ...

  7. jdbc 配置properties实现

    package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...

  8. BZOJ1218 [HNOI2003]激光炸弹

    题目后面写着DP就当它是DP吧.. 本来是扫描线+线段树的说,但是捏5000^2还是能过滴,于是暴力枚举正方形+所谓的DP就解决了. /******************************** ...

  9. PMP 项目管理

    1.什么是项目管理   项目管理就是把各种知识,技能,工具,技术应用于项目活动,来满足项目的需求.这个是从技术方面来说的.其实在项目管理的技术 层面背后,还有理念层面的内容.学习项目管理,除了学习技术 ...

  10. spring mvc与mybatis收集到博客

    mybaits-spring 官方教程 http://mybatis.github.io/spring/zh/ SpringMVC 基础教程 框架分析 http://blog.csdn.net/swi ...