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 ...
随机推荐
- mysql安装三 linux源码安装mysql5.6.22
http://blog.csdn.net/beiigang/article/details/43053803
- [Node.js]DNS模块
摘要 nds模块是node.js用于解析域名的模块,对域名的解析非常快捷方便. DNS 引入dns模块 //引入dns模块 var dns=require("dns"); 方法 序 ...
- Linux内核相关常见面试题
转:http://www.embeddedlinux.org.cn/html/xinshourumen/201303/11-2475.html 本文详细阐述了linux内核相关的开发职位面试中 ...
- MVC批量更新,可验证并解决集合元素不连续控制器接收不完全的问题
在"MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体"中,有2个问题待解决: 1.由jquery动态生成了表单元素,但不能实施验证. 2.一旦集合元素不连续,控制器就 ...
- python文本 去掉字符串前后空格
python文本 去掉字符串前后空格 场景: 去掉字符串前后空格 可以使用strip,lstrip,rstrip方法 >>> a="abc".center (30 ...
- JS浮点数运算
//乘法函数,用来得到精确的乘法结果 //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显.这个函数返回较为精确的乘法结果. //调用:accMul(arg1,arg2) ...
- bat薪酬
三大互联网巨头公司,百度腾讯跟阿里如何划分级别?薪资待遇又有多少?除非身居其位,否则很难探知,但是等你到那个位置知道了,却又不能说,至少不能在公 开场合谈论.接下来就为大家揭秘,百度.阿里与腾讯内部的 ...
- Lucene TFIDF打分公式
还没读TFIDFSimilarity的代码,读了一下lucene的文档,没有特复杂,感觉还是非常严谨的. 对于查询q和文档d,如果查询为纯token查询,套用向量空间模型(VSM),相似度度量使用余弦 ...
- Linux中Shell的执行流程
Shell执行流程 1.Printthe info of reminding 打印提示信息 2.Waitinguser for input(wait) 等待用户输入 3.Acceptthe comma ...
- 笔记本建立wifi热点的实用详细步骤
准备工作: (1) 首先要打开开始菜单--->搜索“services.msc” 并打开(或者用win+R快捷键打开“运行”输入“service.msc”,点击确定)--->找到“WLAN ...