标签:lib 后缀 create amp ant rop cli rest ram
题目链接:https://vjudge.net/contest/70325#problem/H
题目:
ababcababababcabab aaaaaSample Output
2 4 9 18 1 2 3 4 5
题意:给你一个字符串,求出所有前缀后缀相同的长度
思路:一个next数组的应用:整个字符串长度为len时,当i = len时,next[len] =a ,说明整个字符串前a个字符和后a个字符相同,所以a是满足要求的。
当i=next[a]时,next[i]=b,则说明前a个字符的前b个和前a个字符的后b个字符相同,同理整个字符串的后a个的前b个和后b个的字符串相同,则满足整个
字符串的前b个和后b个相同,故b满足,以此类推即可,可用递推求出,可看图发现规律:
// // Created by HJYL on 2019/8/15. // #include <iostream> #include <vector> #include <map> #include <string> #include <queue> #include <stack> #include <set> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> using namespace std; typedef long long ll; const int maxn=1e6+10; char str[maxn]; int nextt[maxn]; void getnextt() { int i=0,j=-1; nextt[0]=-1; int len=strlen(str); while(i<len) { if(j==-1||str[i]==str[j]) { i++,j++; //if(str[i]!=str[j]) nextt[i]=j; // else //nextt[i]=nextt[j]; } else j=nextt[j]; } } int main() { // freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin); while(~scanf("%s",str)) { getnextt(); int len=strlen(str); int a[maxn]; int pos=0; int j=len,b=0; for(int i=1;i<=len;i++) { b=nextt[j]; if(b>0) { a[pos++] = b; j = nextt[j]; } } for(int i=pos-1;i>=0;i--) printf("%d ",a[i]); printf("%d\n",len); } return 0; }
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)
标签:lib 后缀 create amp ant rop cli rest ram
原文地址:https://www.cnblogs.com/Vampire6/p/11361367.html