POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串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函数,求前缀和后缀的匹配长度)的更多相关文章
- POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)
题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...
- 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 ...
- 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 ...
- 「LOJ#10036」「一本通 2.1 练习 2」Seek the Name, Seek the Fame (Hash
题目描述 原题来自:POJ 2752 给定若干字符串(这些字符串总长 ≤4×105 \le 4\times 10^5 ≤4×105),在每个字符串中求出所有既是前缀又是后缀的子串长度. 例如:abab ...
- POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
题目链接:http://poj.org/problem?id=2752 题目大意:给你一个字符串 \(S\) ,如果它的一个前缀同时也是它的后缀,则输出这个前缀(后缀)的长度. 题目分析:next函数 ...
- (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 ...
- 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的简单应 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- 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 ...
随机推荐
- VC中实现GCC的2个比较常用的位运算函数
在GCC中内嵌了两个位运算的函数,但在VC中并没有这两个函数(有相似函数). //返回前导的0的个数. int __builtin_clz (unsigned int x) //返回后面的0个个数,和 ...
- shelll函数求两个输入数字之和
#!/bin/bash #This is a test of the addition of the program! function AddFun { read -p "Enter a ...
- Media Player(APP)
今天共享一下,以前做的影音播放器.比较简单.适合新手. 我上传了附件可以参考一下. PDF:http://files.cnblogs.com/files/hongguang-kim/MediaPlay ...
- KnockoutJS学习笔记10:KonckoutJS foreach绑定
KnockoutJS foreach绑定用来处理数组,通常用来将一个数组绑定到一个列表或者table中.在foreach绑定中,我们可以使用if.with等嵌套绑定. 示例代码: <tabl ...
- maven学习手记 - 1
学习目标 windows下安装maven环境: 使用命令创建maven项目结构: maven项目编译测试打包安装运行: 在maven项目中使用插件. 在windows下安装maven环境 在win ...
- 解决vs2013不能添加控制器的步骤
点击:vs2013 更新,更新完以后,再重启下电脑就可以正常使用了
- c/c++常用代码--udp多播
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winsock.h ...
- iOS 进阶 第十四天(0416)
0416 注意调用关系,如下图: 就是initWithCoder:.initWithFrame.setup方法三元组
- unity2d之2d帧动画创建
在2d游戏中帧动画的应用是非常广泛的,那么如何在unity中创建一个帧动画,主要是下面几个步骤. 原文地址 :http://blog.csdn.net/dingkun520wy/article/de ...
- 基于.net mvc的校友录(五、web.config对的配置以及filter实现的权限控制)
web.config配置文件 此文件是整个系统的配置中心,它告诉iis服务器本网站需要哪些运行时环境,需要哪些环境,将要进行哪些操作,开发人员也会将一个常量性的数据放在此配置中,以备系统全局调用.此文 ...