POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点。
分析:next数组的一种很巧妙的用法
next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等
所以就会有xy=ab(用xy表示x - y的这一段),则next[b]=y,那么下次就从y这个位置开始匹配
如果xk=wy,因为xy=ab,故wy=lb,所以xk=lb,就得到了前缀和后缀相等。
详细见:http://www.mamicode.com/info-detail-977140.html
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff
char s[400010];
int next[400010];
void get_next(){
int i,j,len = strlen(s);
next[0] = 0;
next[1] = 0;
for(i=1;i<len;i++){
j = next[i] ;
while(j && s[i] != s[j]) j = next[j] ;
if(s[i] == s[j]) next[i+1] = j+1 ;
else next[i+1] = 0 ;
}
}
int main(){
int i,j,num[400000],cnt;
while(scanf("%s",s) == 1){
cnt = 0;
get_next();
int len =strlen(s);
j = len -1 ;
while(j){
i = j;
j = next[j];
while(j && s[i] != s[j]) j = next[j] ;
if(s[i] == s[j]) num[cnt++] = j+1 ;
else break ;
}
sort(num,num+cnt);
for(i=0;i<cnt;i++)
printf("%d ",num[i]);
printf("%d\n",len);
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)的更多相关文章
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- (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 ...
- 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 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- WPF 控件之ComboBox绑定[2]
最近感觉新的方法Binding comboBox用起来很好用. 记录一下: <ComboBox Grid.Row=" x:Name="cboFamilyName" ...
- 解决 在IE与firefox宽度不一致的问题
浏览器默认不同的字体问题,字体分为“等宽”和“不等宽”字体,所以 在IE与firefox内间距是不等的. 解决办法: body{font-family: 宋体, simsun; ...
- python网络请求简洁之道--python requests简介
#requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...
- Java 高效 MVC & REST 开发框架 JessMA v3.2.1 即将发布
JessMA(原名:Portal-Basic)是一套功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 ...
- php中对MYSQL操作之批量运行,与获取批量结果
<?php //批量运行,与获取结果 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysqlusername". ...
- CSS基础知识之float
前段时间写过一篇CSS基础知识之position,当时对float的理解不太准确,被慕课网多名读者指出(原文已修正,如有误导实在抱歉).现对float进行更深入的学习,在此把学习心得分享给大家. 浮动 ...
- ASP.NET MVC请求处理过程
- 后台写js 并跳转
Response.Write("<script>alert('成功');location.replace('ApplyClass.aspx')</script>&qu ...
- 如何安装Windows Live Writer插件
Windows Live Writer 是一个强大的离线博客编辑工具,通过它可以离线编辑内容丰富的博文.它不但支持微软的live space,还支持诸如Wordpress 这样的开源博客系统. Win ...
- Find The Multiple
算法:深搜(水题): Given a positive integer n, write a program to find out a nonzero multiple m of n whose d ...