题意:给一个字符串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 数组 变形)的更多相关文章

  1. 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 ...

  2. (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 ...

  3. 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的简单应 ...

  4. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

    Seek the Name, Seek the Fame Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 24000 ...

  9. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

随机推荐

  1. html--鼠标控制DIV移动

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. [置顶] ProDinner体验

    最近研究了MVC的经典案例ProDinner. 下载地址是:http://prodinner.codeplex.com/ 部署完毕后,看看效果怎么样: Meals的多选功能非常不错: Meal界面格外 ...

  3. AVR32开发环境搭建

    下面是搭建AVR32开发环境的过程记录: 1.AVR32的编译环境下载  (到这里下载  as5installer-stable-5.1.208-full.exe) 如果你在安装的过程中碰到如下问题: ...

  4. IOS开发之格式化日期时间

    IOS开发之格式化日期时间(转)   在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如: //实例化一个NSDateFor ...

  5. C# 对象深复制

    Mark: //实现IClonable接口并重写Clone方法就可以实现深克隆了 #region ICloneable 成员 public object Clone() { MemoryStream ...

  6. Material Calendar View 学习记录(二)

    Material Calendar View 学习记录(二) github link: material-calendarview; 在学习记录一中简单翻译了该开源项目的README.md文档.接下来 ...

  7. mysql 超时 问题处理

    当数据库出现10055和10048错误时,处理办法: 第一 链接超时设置(1)打开注册表:regedit 找到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ ...

  8. 高效的jQuery代码编写技巧总结

    最近写了很多的js,虽然效果都实现了,但是总感觉自己写的js在性能上还能有很大的提升.本文我计划总结一些网上找的和我本人的一些建议,来提升你的jQuery和javascript代码.好的代码会带来速度 ...

  9. [Mugeda HTML5技术教程之14]案例分析:制作网页游戏

    本文档要分析的案例是一个爱消除的网页小游戏,从中可以体会一些Mugeda API的用法和使用Mugeda动画制作网页游戏的方法. (一)游戏规则: 1.开始游戏时,手机出现在最上面一行的任意一格: 2 ...

  10. python Tips(不定期更新)

    dictionary sort 1.根据key排序,正向排序 sorted(dic.items(), key=lambda d: d[0]) 2.根据value排序,反向排序 sorted(dic.i ...