poj2752 Seek the Name, Seek the Fame(next数组的理解)
来源:互联网 发布:me的域名 编辑:程序博客网 时间:2024/05/17 18:19
题目:
在一个字符串中,输出那些既是后缀又是前缀的字符串的长度。
分析:
这个题就看next数组理解的深不深刻。
next其实是给字符串划分了对称性,大对称中套着小对称,小对称套着更小的对称。
两段长度为k的值是完全相同的,而两段k中各分为两段后,四段小蓝色部分也是相同的。这是个重要的性质。
nxt[j]就是长度为k的那一截,nxt[k-1]就是其中一截蓝色的。“递归”下去即可。
代码:
#include <iostream>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define ms(a,b) memset(a,b,sizeof(a))#define lson rt*2,l,(l+r)/2#define rson rt*2+1,(l+r)/2+1,rtypedef unsigned long long ull;typedef long long ll;const int MAXN=4e5+5;const double EPS=1e-8;const int INF=0x3f3f3f3f;char s[MAXN];int n,nxt[MAXN];void getnext(){ nxt[0] = 0; for(int i=1;i<n;i++){ int j = nxt[i-1]; while(s[j] != s[i] && j>0){ j = nxt[j-1]; } if(s[j] == s[i]) nxt[i] = j+1; else nxt[i] = 0; }}int main(){ while(~scanf("%s",s)){ n = strlen(s); getnext(); int ans[MAXN]; int l = n,k=0; while(l > 0){ ans[k++] = l; l = nxt[l-1]; } for(int i=k-1;i>=0;i--){ printf("%d%c",ans[i]," \n"[i==0]); } } return 0;}
阅读全文
0 0
- poj2752 Seek the Name, Seek the Fame(next数组的理解)
- POJ2752 Seek the Name, Seek the Fame next数组应用
- poj2752 Seek the Name, Seek the Fame(next数组的运用)
- POJ2752 Seek the Name, Seek the Fame KMP-next数组的应用
- Seek the Name, Seek the Fame(KMP算法之next数组的深入理解实例)
- Seek the Name, Seek the Fame next数组的应用
- POJ2752 Seek the Name, Seek the Fame
- POJ2752:Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name seek the fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752-Seek the Name, Seek the Fame
- poj2752 Seek the Name,Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name,seek the fame
- POJ2752 Seek the Name, Seek the Fame
- 原型模式|设计模式
- python的数据挖掘--YongGG
- js中的事件委托或是事件代理详解
- hdu 5761 How Many Triangles 极角排序
- MyEclipse各版本复制项目时,更改访问路径总结
- poj2752 Seek the Name, Seek the Fame(next数组的理解)
- Java-Java I/O流解读之java.io.PrintStream & java.io.PrintWriter
- hdu5791Two dp hdu5781ATM Mechine 概率dp hdu5787 K-wolf Number 数位dp
- Glide的简单封装GlideUtils
- Codeforces Round #433 (Div. 2) E. Boredom (主席树 计算矩形内点的个数)
- Java基础5:基本数据类型
- sql server 2008还原和备份
- hdu 5875 Function 线段树||RMQ
- hdu 5795A Simple Nim sg打表