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 ...
随机推荐
- Codeforces Round #128 (Div. 2)
A. Two Problems 分两题的过题情况讨论,并且数值均不大,暴力枚举. B. Game on Paper 每次给格子染色后,考虑当前格子在正方形中的位置,然后判断对应的正方形是否都已染色. ...
- 团队项目开发中,常见的版本控制有svn,git
团队项目开发中,常见的版本控制有svn,git
- Processor Speculative & pipeline
https://en.wikipedia.org/wiki/Instruction-level_parallelism https://en.wikipedia.org/wiki/Instructio ...
- python2.7处理https稍微好点的办法(坑得一笔)
from warnings import filterwarnings filterwarnings('ignore') r = requests.get(url, headers=headers, ...
- CSS浮动讲解好文章推荐
经验分享:CSS浮动(float,clear)通俗讲解 http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html 好文推荐!
- API、ABI区别
http://blog.csdn.net/xinghun_4/article/details/7905298 应用程序二进制接口(ABI-Application Binary Interface)定义 ...
- OpenJudge 计算概论-判断闰年
/*======================================================================== 判断闰年 总时间限制: 1000ms 内存限制: ...
- 无法启动:此实现不是Windows平台FIPS验证的加密算法的一部分
个别同学可能会在启动订票助手.NET的时候发现这个提示: 出现这个问题的原因是订票助手.NET使用了MD5算法,而系统的组策略安全设置导致无法使用此算法.要修正此问题,请按照如下操作(两种方法任选其一 ...
- 使用bind(this)的情况
1.setInterval().setTimeout()的回调函数,一定要加.bind(this)方法. 原因是:在setInterval()中定义的回调函数,是在同步代码执行完后,随着事件触发来异步 ...
- MySQL 使用mysqld_multi部署单机多实例详细过程 (转)
随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然 5.6版本已经在这方面改进很多,但是也没有达到100%,所以为了充分的利用系统 ...