给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀。升序输出所有情况前缀的长度。
KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度。
明白了next[i],那么这道题就很容易做了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std;
const int maxn=;
char str[maxn];
int next[maxn];
int n;
int ans[maxn];
void getnext(char *str,int len){
next[]=-;
int i=,j=-;
while(i<len){
if(j==- || str[i]==str[j]){
i++;j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(scanf("%s",str)!=EOF){
n=strlen(str);
getnext(str,n);
int l=next[n];
int idx=;
ans[idx++]=n;
while(l!=){
ans[idx++]=l;
l=next[l];
}
for(int i=idx-;i>=;i--){
if(i==idx-)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
}
return ;
}

POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)的更多相关文章

  1. POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

    题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...

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

  3. POJ:2753-Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...

  4. 「LOJ#10036」「一本通 2.1 练习 2」Seek the Name, Seek the Fame (Hash

    题目描述 原题来自:POJ 2752 给定若干字符串(这些字符串总长 ≤4×105 \le 4\times 10^5 ≤4×105),在每个字符串中求出所有既是前缀又是后缀的子串长度. 例如:abab ...

  5. POJ2752 Seek the Name, Seek the Fame 题解 KMP算法

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一个字符串 \(S\) ,如果它的一个前缀同时也是它的后缀,则输出这个前缀(后缀)的长度. 题目分析:next函数 ...

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

  7. 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的简单应 ...

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

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

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

随机推荐

  1. [笔记]--Sublime Text 2使用技巧

    Sublime个人喜好设置: 在打开个人设置页面Preferences >> Settings - User,加入以下内容: { , //TAB键,4个空格 "translate ...

  2. java的软件包

    Java的软件包:简单来说,软件包就是把类放在不同的文件夹下,提供了命名空间 package wang; //用package将Test类放在wang文件下 class Test{ public st ...

  3. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  4. button与submit

    原文来自: http://blog.sina.com.cn/s/blog_693d183d0100uolj.html submit是button的一个特例,也是button的一种,它把提交这个动作自动 ...

  5. EMVTag系列16《AC响应数据》

    在一个联机交易中,要传送到发卡行的专有应用数据. 字段 长度(字节) 赋值 说明 长度 1 07 分散密钥索引 1 00 密文版本号 1 01 根据发卡行密钥版本设置 卡片验证结果(CVR) 4 03 ...

  6. ListView的多布局中的小问题

    今天用到了ListView的多布局,我们需要额外重写两个方法 //返回多布局的个数 @Override public int getViewTypeCount() { return 3; } //用该 ...

  7. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  8. 一位iOS教育类应用开发者是如何赚到60多万美元?

    注:伯乐在线12月19日在@程序员的那些事 微博推荐了此文的英文原文,非常感谢@dotSlash 的翻译.  转眼距我写<我如何在iOS教育类应用中赚到20万美元>这篇博文已经一年多了,它 ...

  9. php匿名函数小示例

    <?php //$fun = function($params){ // echo $params; //}; // //$fun('aa'); //例一 //在普通函数中定义一个匿名函数 // ...

  10. Linux虚拟机配置本地yum源

    刚开始使用Linux,自己构建了一个Linux虚拟机之后,在使用yum install的时候,经常是出错,提示连接不上. 一直以为是自己构建的虚拟机的问题,后来在网上查找了一些资料,才发现:需要配置本 ...