Seek the Name,Seek the Fame

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

【题目链接】Seek the Name,Seek the Fame

【题目类型】KMP

&题意:

给你一个字符串S。假如为ababcababababcabab 找出这个字符串中所有的前缀等于后缀的子串,输出它们的长度。

第一个为a,最后一个为b,所以1不行。

前两个为ab,最后二个也为ab,所以2行。

前三个为aba,后三个为bab,所以3不行。

以此类推... 找出全部

当然,全部字符串也是可以的。因为ababcababababcabab和ababcababababcabab肯定是一样的。

&题解:

参考:http://blog.csdn.net/niushuai666/article/details/6968507

这道题也是next数组的一个应用。

既然是求模式串匹配,所以要先求出模式串的next数组。这是第一步。

然后我们开始分析怎么用next数组来完成查找前缀后缀的匹配。

我们还是用上面那个字符串为例子来说明一下。

下标 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

模式串 a b a b c a b a b a b a b c a b a b

next[i] -1 0 0 1 2 0 1 2 3 4 3 4 3 4 5 6 7 8 9

1)当i = len时,next[len] = next[18] = 9,说明整个字符串前9个字符和后9个字符相同,所以9是满足要求的。

2)next[9] = 4,说明在0-8中前4个字符和后4个字符相同。因为由1)得前9个字符和后9个字符相同,所以,S串的0-3等于5-8,而0-3又等于9-12,5-8又等于14-17,所以结果是0-3等于14-17,即4也是满足题意的。

3)next[4]=2,同2,我们可以得到2也是满足题意的。

4)next[2]=0,表明没有相同的前缀和后缀了,这时,就已经找到了这个S串的所有前缀和后缀。

5)结果就是2,4,9,18.

所以,我们可以推得这样的结论:凡是next[i]!=0的,都是模式串的前缀和后缀相同的字符数。

这也是getnext函数的一个重要应用,多理解理解就可以了。

也就是说nex数组中实在的意义就是前缀有重复的,这题又通过相等的传递性,从前缀通过中间的相等传到最后的后缀,从而解决了问题

【时间复杂度】\(O(m+n)\)

&代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const int maxn = 400000 + 9 ;
int nex[maxn],n,ans[maxn];
char s[maxn];
void kmpN(char* x,int len) {
int i=0,j=nex[0]=-1;
while(i<len) {
while(j!=-1&&s[i]!=s[j])j=nex[j];
nex[++i]=++j;
}
}
void Solve() {
while(~scanf("%s",s)) {
n=strlen(s);
kmpN(s,n);
// printf("%s\n", s);
// rep(i,0,2)printf(" ");
// rep(i,0,n-1) {
// printf(" %c",s[i] );
// }
// PU();
// rep(i,0,n) {
// printf("%3d",nex[i] );
// }
// PU();
int ct=0;
int t=n;
while(t!=0) {
ans[ct++]=t;
t=nex[t];
}
red(i,ct-1,0) {
printf("%d%c",ans[i],i==0?'\n':' ');
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin),freopen("1.out","w",stdout);
#endif
//iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// int T;cin>>T;while(T--)
Solve();
return 0;
}

POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)的更多相关文章

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

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

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

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

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

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

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

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

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

  8. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

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

  9. 题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

随机推荐

  1. centos7安装zabbix客户端并监控

    zabbxi-agent安装及配置 1.安装zabbxi-agent yum install zabbix-agent -y 2.配置zabbxi-agent grep -n '^'[a-Z] /et ...

  2. Apache Kafka - KIP-42: Add Producer and Consumer Interceptors

    kafka 0.10.0.0 released   Interceptors的概念应该来自flume 参考,http://blog.csdn.net/xiao_jun_0820/article/det ...

  3. 【数据库】Invalid default value for 'create_date' timestamp field

    问题 最近遇到一个这样的问题,新建数据库表的时候 提示 错误如下 Invalid default value for 'created_time' timestamp field 语句如下 `crea ...

  4. 第一版STM32PCB的问题

    1.5与6引脚连在一起了 2.有3个插排隔得太近,导致传感器安装不了 3.开关不匹配,画PCB时可以考虑一下实验室有哪些东西 4.可以用开关代替跳冒 5.AM3.3电路电容 6.钽电容的正负极区分 有 ...

  5. java之旅_高级教程_java泛型

    摘自:http://www.runoob.com/java/java-generics.html JAVA泛型 java泛型(generics)是JDK5中引入的新特性,泛型提供了编译时类型安全检测机 ...

  6. xpath定位方法详解

    1.xpath较复杂的定位方法: 现在要引用id为“J_password”的input元素,可以像下面这样写: WebElement password = driver.findElement(By. ...

  7. sublime 可能卡的原因

    在重新安装编辑器的时候,配置javascript提示,发现这个插件安装之后,sublime变得非常卡,按照我电脑的性能,同时开着的atom敲代码都不卡.于是把这个插件卸载之后,sublime变得非常流 ...

  8. python-面向对象-14_eval函数

    eval 函数 eval() 函数十分强大 —— 将字符串 当成 有效的表达式 来求值 并 返回计算结果 # 基本的数学计算 In [1]: eval("1 + 1") Out[1 ...

  9. Python3学习之路~3.1 函数基本语法及特性、返回值、参数、局部与全局变量

    1 函数基本语法及特性 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 语法定义: d ...

  10. dxRangeTrackBar使用教程

    Properties: Max:最大值 Min:最小值 Frequency:设置刻度值多大值显示PageSize:选择时跳动的区域大小 SelectionColor:选择区域颜色 ShowSelect ...