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 ...
随机推荐
- vc 6.0 远程调试
http://blog.sina.com.cn/s/blog_45eaa01a01014eb5.html
- JQUERY AJAX无刷新异步上传文件
AJAX无刷新上传文件并显示 http://blog.csdn.net/gao3705512/article/details/9330637?utm_source=tuicool jQuery For ...
- 四种DCOM错误的区别,0x80080005 0x800706be 0x80010105 0x
四种DCOM错误的区别Differences between the following DCOM error 0x800800050x800706be0x800101050x800706ba ...
- 分析oracle索引空间使用情况,以及索引是否须要重建
分析索引空间使用情况.以及索引是否须要重建 分析其它用户下的索引须要 analyze any的权限 分析索引前先查看表的大小和索引的大小,假设索引大小和表大小一样大或者大于表的大小,那么能够推断索引可 ...
- Java clone方法(下)
1.终于调用的是一个JNI方法,即java本地方法,加高速度 2.使用clone方法,分为浅复制.深复制,这里直接使用网上抄来的案例来说明吧: 说明: 1)为什么我们在派生类中覆盖Object的clo ...
- 在Windows下编译Emacs
在Windows下编译Emacs Windows下编译好的Emacs主要有两个版本,一个来自http://nqmacs.sourceforge.net/,另一个来自http://www.crasseu ...
- android破解
1.adb shell dumpsys activity top 能够获取到当前程序的Activity信息 2.1.在invoke-static/invoke-virtual指令他的返回类型是V之后可 ...
- HDU1874畅通project续 dijkstra&&floyd
畅通project续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Time Limit: 3000/1000 MS (Java/Others) ...
- java读取文件并获得文件编码,转换为指定编码的工具类代码
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- mybatis映射文件遇到的小问题
mybatis的映射文件插入操作时: 如果对应的属性是String类型的,那么一定要做空串的判断. 比如注册的时候,如果需要向数据库中插入一条记录时,对应的字段没有给他赋值,这个String类型的值传 ...