POJ--2752--Seek the Name, Seek the Fame【KMP】
id=2752
题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值。
思路:这道题加深了对next数组的理解。next[i+1]相当于以第i位结尾的长度为next[i+1]的子串与前next[i+1]个字符组成的子串同样。理解之后就比較好做了,首先字符串的长度len肯定是一个答案。然后next[len]也是一个答案,原因如红字所写,如此迭代直到next下标值等于0停止。这是从大到小得到了答案。再反序输出就可以。
由于这道题的特殊性,用优化的getnext函数无法AC。
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 500100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define mod 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 char str[401000];
int next[401000];
void getnext(){
int i = 0, j = -1;
int l = strlen(str);
next[0] = -1;
while(i<l){
if(j==-1||str[i]==str[j]){
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
int ans[401000];
int main(){
int i,j;
while(scanf("%s",str)!=EOF){
getnext();
int l = strlen(str);
int i = l;
int sum = 0;
while(i!=0){
ans[sum++] = i;
i = next[i];
}
printf("%d",ans[sum-1]);
for(i=sum-2;i>=0;i--){
printf(" %d",ans[i]);
}
puts("");
}
}
POJ--2752--Seek the Name, Seek the Fame【KMP】的更多相关文章
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- poj2752seek the name, seek the fame【kmp】
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...
- POJ_2752 Seek the Name, Seek the Fame 【KMP】
一.题目 POJ2752 二.分析 比较明显的KMP运用. 但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀. 可以不断的运用KMP得出答 ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- POJ 2976 Dropping tests:01分数规划【二分】
题目链接:http://poj.org/problem?id=2976 题意: 共有n场考试,每场考试你得的分数为a[i],总分为b[i]. 你可以任意去掉k场考试. 问你最大的 100.0 * ( ...
- 【KMP】POJ 2185 Milking Grid -- Next函数的应用
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- POJ 3264 Balanced Lineup(模板题)【RMQ】
<题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...
随机推荐
- Spring安全权限管理(Spring Security)
1.spring Security简要介绍 Spring Security以前叫做acegi,是后来才成为Spring的一个子项目,也是目前最为流行的一个安全权限管理框架,它与Spring紧密结合在一 ...
- spring事務
spring事物 spring事物其实就是对数据库事物的一种支持,没有数据库事物的话,spring本身是不能提供事物支持的: 在最开始使用原始的jdbc连接数据库进行炒操作是, 获取连接后可以使用co ...
- ndk 开发
5.用NDK来编译程序 1. 现在我们用安装好的NDK来编译一个简单的程序吧,我们选择ndk自带的例子hello-jni,我的位于E:/android-ndk-r5/samples/hello-jn ...
- iOS News Reader开源项目
项目介绍 NewsReader项目是一个新闻阅读类开源项目,支持iPhone和iPad. 目前基于该项目并且已发布到App Store的产品有: VOA慢速英语和每天6分钟英语等 项目源码地址:htt ...
- pytest文档18-配置文件pytest.ini
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...
- python接口自动化4-绕过验证码登录(cookie)
前言 有些登录的接口会有验证码:短信验证码,图形验证码等,这种登录的话验证码参数可以从后台获取的(或者查数据库最直接). 获取不到也没关系,可以通过添加cookie的方式绕过验证码. 一.抓登录coo ...
- 优化JDBC编程-多提提意见
优化JDBC编程这是我根据MS SQL SERVER 2000 JDBC DRIVER HELP,并参考其它资料整理而成.ms的这个帮助文件实在有失大家风范,示例代码很.....有兴趣者可以去下载ht ...
- FizzBuzzWhizz问题python解法
FizzBuzzWhizz 你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏.此时有100名学生在上课.游戏的规则是: 1. 你首先说出三个不同的特殊数,要求必须是个位数,比方3.5. ...
- 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes
This tutorial is available as a short ebook. The e-book features extra content from follow-up posts ...
- 常用的OpenCV函数速查
常用的OpenCV函数速查 1.cvLoadImage:将图像文件加载至内存: 2.cvNamedWindow:在屏幕上创建一个窗口: 3.cvShowImage:在一个已创建好的窗口中显示图像: 4 ...