题目链接:https://vjudge.net/contest/70325#problem/F
题目:
bcabcab efgabcdefgabcdeSample Output
3 7
题意:求最小循环节的长度
思路:kmp中n-next[n]就是最小循环节的长度,由于现在该题不支持提交,我就贴一下我写的
// // 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; const int maxn=1e6+10; char str[maxn]; int nextt[maxn]; void getnext() { int i=0,j=-1; nextt[0]=-1; int n=strlen(str); while(i<n) { 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)) { getnext(); int len=strlen(str); printf("%d\n",len-nextt[len]); } return 0; }
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher F - The Minimum Length HUST - 1010 (kmp循环节)
原文:https://www.cnblogs.com/Vampire6/p/11360768.html