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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- day54
今天复习时间15个小时 那都做了什么呢 数学2000试卷 阅读2篇整理 翻译2个视频 政治背诵加视频 数学综合5个证明 作文两篇 c语言结构体以及简单总结 博客园日记 数据结构 好了 感觉也没有做什么 ...
- PC-CSS-多浏览器支持HTML5
非IE:article, section, aside, hgroup, nav, header, footer, figure, figcaption {display: block;}IE:< ...
- c语言学习之结构篇代码演示样例-输入n个同学的姓名,数学英语成绩,依照平均分从低到高排序并输出
#include<stdio.h> void main(){ const int count = 5;//定义数量 struct student{ char name[80]; float ...
- 使用Win32::OLE操作Excel——Excel对象模型
像VBA操作Excel一样,Win32::OLE模块也是通过对象操作来控制Excel. 如果想自动化操作和控制Excel应用程序,则必须要与Excel对象模型所提供的对象进行交互.理解和熟悉Excel ...
- 关于sed的应用
公司让我做一个看一下在优化的程序和比原来的程序快多少,但是文件还在运行的服务器上,我需要把用到的文件复制到测试服务器上去.但是测试服务器上有的,目录不全,会导致scp出错.就发生了以下的故事. 首选我 ...
- 超长英文(代码)自动换行的样式(CSS)
如何想让一连串文字在显示可以自动换行,而不会把在代码中使用的容器撑开,则在文章的CSS样式处加上以下代码即可: table-layout: fixed; word-wrap:break-word;或者 ...
- DEV GridControl 获取选中行的数据
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowCha ...
- Windows消息机制(转)1
Windows的应用程序一般包含窗口(Window),它主要为用户提供一种可视化的交互方式,窗口是总是在某个线程(Thread)内创建的.Windows系统通过消息机制来管理交互,消息(Message ...
- Android的启动模式
启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的<Activity>标签的android:launchMode属性设置: 启动模式有4种,分别 ...
- kafka的一些常用命令
启动zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties & 启动kafka bin/kafka-server ...