POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)
题意:
给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度。
这个就是next数组的应用,next数组真是很深奥啊。
根据最后一个next数组的值,递归去找前面的值,直到是0时停止。证明见链接。
链接:http://www.cnblogs.com/dongsheng/archive/2012/08/13/2636261.html
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
;
/* ///////////////////////// C o d i n g S p a c e ///////////////////////// */
+ ;
int nnext[MAXN];
char s[MAXN];
int sum[MAXN];
void getnext(int len)
{
int i,j;
i=;
j=-;
nnext[]=-;
while(i<len)
{
||s[i]==s[j])
{
++i,++j;
nnext[i]=j;
}
else
{
j=nnext[j];
}
}
}
int main()
{
int len,i,k;
while(scanf("%s",s)!=EOF)
{
k=;
len=strlen(s);
getnext(len);
;)
{
sum[k++]=nnext[i];
i=nnext[i];
}
; i>=; --i)
printf("%d ",sum[i]);
printf("%d\n",len);
}
;
}
第二次,自己A
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define PU puts("");
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
;
/* ///////////////////////// C o d i n g S p a c e ///////////////////////// */
+ ;
int Next[MAXN];
int len;
char x[MAXN];
int d[MAXN],cntd;
void kmp_pre()
{
int i,j;
j=Next[]=-;
i=;
while(i<len)
{
!=j&&x[i]!=x[j])j=Next[j];
Next[++i]=++j;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif
while(~scanf("%s",x))
{
cntd=;
len=strlen(x);
kmp_pre();
// for (int i=1;i<=len;i++) printf("%3d ",i);PU
// for (int i=1;i<=len;i++) printf("%3d ",Next[i]);PU
;)
{
d[cntd++]=Next[i];
i=Next[i];
}
;i>=;i--)
{
printf("%d ",d[i]);
}
printf("%d\n",len);
}
;
}
POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)的更多相关文章
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- (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 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- 题解报告: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 ...
随机推荐
- URAL 1080 Map Coloring(染色)
Map Coloring Time limit: 1.0 secondMemory limit: 64 MB We consider a geographical map with N countri ...
- POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)
Destroying The Graph Time Limit: 2000MS Memo ...
- hdu5443(2015长春赛区网络赛1007)暴力
题意:给了一个数列,有多个询问,每个询问求某个区间内的最大值 数列长度 1000,询问个数 1000,静态,并不需要RMQ这些,直接暴力 n2 查找每个询问区间取最大值就行了. #include< ...
- 关闭V-Ray warning消息框
有时候模型使用低版本VR保存的,再次打开模型时会弹出V-Ray warning提示框 这个问题困扰了我一周时间.... 查了VR官方帮助文档 解决方法如下 setVRaySilentMode() -- ...
- android基础知识13:AndroidManifest.xml文件解析
注:本文转载于:http://blog.csdn.net/xianming01/article/details/7526987 AndroidManifest.xml文件解析. 1.重要性 Andro ...
- 在CentOS里使用MySQL Connector/C++
操作系统版本:CentOS6 64位 1,安装boost库.因为MySQL Connector/C++使用了boost库,所以必须先安装boost库,我们才能使用MySQL Connector/C++ ...
- asp.net MVC 帮助助手和函数( @helper @functions)
asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...
- html之内联标签img
img创建的是被链接图像的占位空间,它不会真正地在网页插入图像. 两个必须属性: src:图像的超链接 alt:图像的替代文本 可选属性: height:高度 width:宽度 ismap:将图像定义 ...
- android 实现拍照的2种方法
android系统的照相功能,已实现2种方法,可供大家参考: 1.调用系统摄像头来拍照 首先,找到AndroidManifest.xml文件里加入用户权限 <uses-permission an ...
- 解决PHP 5.6.11中cURL模块问题!
按照网上的教程写了一个cURL的小例子,在apache环境下执行,一点反应也没有,放在IIS环境里就ok的,感觉问题一定出在动态连接库上,因为配置文件里的php_curl.dll已经打开了,而且在ii ...