生活随笔
收集整理的這篇文章主要介紹了
POJ - 1961 最小循环节
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
如果循環(huán)節(jié)存在那在前綴部分也肯定存在
如果循環(huán)節(jié)存在那至少是可以匹配的
而next是維護(hù)最大前綴的,意會意會
注意一定要先判整除,即使別的題目保證是存在循環(huán)的
特意畫了一張靈魂草圖幫助理解
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){ll x=0,f=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
int nxt[maxn];
char P[maxn];
void buildNext(){nxt[1]=0;int j=0,m=strlen(P+1);rep(i,2,m){while(j&&P[i]!=P[j+1])j=nxt[j];if(P[i]==P[j+1])j++;nxt[i]=j;}
}
int main(){int m,kase=0;while(~iin(m)){if(m==0)break;s1(P);buildNext();printf("Test case #%d\n",++kase);rep(i,2,m){if(i%(i-nxt[i])==0&&i/(i-nxt[i])>1){print(i); blank;println((i/(i-nxt[i])));}}putchar('\n');}return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/caturra/p/8436239.html
總結(jié)
以上是生活随笔為你收集整理的POJ - 1961 最小循环节的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。