做此题,只要理解好next数组就行.......................

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
char str[411111],tmp[411111];
int next[411111],ans[411111]; void getnext(char *t) {
int i=0,j=-1;
int len = strlen(t);
next[0] = -1;
while(i < len) {
if(t[i] == t[j] || j == -1) {
i ++;
j ++;
next[i] = j;
} else j = next[j];
}
}
/*
int kmp(char *s,char *t) {
int lens = strlen(s);
int lena = strlen(t);
int i=0,j=0;
while(i < lens && j < lena) {
if(s[i] == t[j] || j == -1) {
i++;
j++;
} else j = next[j];
}
if(j < lena) return -1;
return i - lena;
}
*/
int main() {
while(scanf("%s",str) != EOF) {
strcpy(tmp,str);
int len = strlen(str);
int cnt = 0;
getnext(str);
int j = next[len];
ans[cnt++] = j;
while(j > 1) {
ans[cnt++] = next[j];
j = next[j];
}
for(int i=cnt-1; i>=0; i--) {
if(ans[i] != 0)
printf("%d ",ans[i]);
}
printf("%d\n",len);
}
return 0;
}

POJ 2752 Seek the Name, Seek the Fame(next数组的理解)的更多相关文章

  1. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  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. BestCoder Round 59 (HDOJ 5500) Reorder the Books

    Problem Description dxy has a collection of a series of books called “The Stories of SDOI”,There are ...

  2. 配置Windows Server 2008 允许多用户远程桌面连接

    开启远程桌面后,远程访问windows server 2008服务器时,默认只支持一个用户名同时只能创建一个远程连接,新建连接登录后会将前一个就踢掉,有没有办法像windows server 2005 ...

  3. js的相关验证

    1 var JavaScriptCommon = { /*身份证号码校验*/ VerifyID: function (socialNo) { if (socialNo == "") ...

  4. union关键字 与大小端模式

    union 关键字(主要用来压缩空间,如果一些数据不可能同一时间同时用到,可是考虑使用union) union关键字声明的变量称之为联合体变量: (1)联合体变量只配置一个足够大的空间来容纳最大长度的 ...

  5. 自己动手写框架——IoC的实现

    先看看 IoC百度百科 优化过程 namespace Test { class Program { static void Main(string[] args) { //场景 某公司客服要回访一些客 ...

  6. vs2012远程调试

    不知道大家有没有遇到过这种情况,刚开发完的程序,明明在本机能够好好的运行,可是部署到服务器过分发给用户时,总是出现莫名其妙的错误. 一时半会又看不出问题来,怎么办呢?难道只能在服务器或是客户电脑上装一 ...

  7. django 学习 --- 环境搭建

    1 安装django a: pip安装 pip install Django==版本号 b:源码安装 https://www.djangoproject.com/download/ tar -xvzf ...

  8. phpcms v9二次开发之模型类的应用(2)

    二.模型操作方法select()--查询语句         //查询级别管理列表信息    public function levellists() { $lelists = $this->l ...

  9. 【学习笔记】【oc】Block

    块(block):类似于定义一个匿名的函数.至于其他什么用处我就不知道了 块的定义: ^[块返回值类型](形参类型1 形参1, 形参类型2 形参2,...) { //块执行体 } 跟函数语法格式的差别 ...

  10. Reachability 检测网络状态

    -(void)viewWillAppear:(BOOL)animated { [IOSExcept JudgeNetwork];//联网 NSLog(@"检查网络 请稍后....." ...