UVA10298 Power Strings

Jisoo

枚举一下next也没事

也可以运用结论

这个结论画个图证明就可以了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
template<class T>inline void read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}
char s[1000006];
int ne[1000006];
int ma; 
int main(){
	ios::sync_with_stdio(false);
	while(scanf("%s",s+1)){
		if(s[1]=='.') return 0;
		int l=strlen(s+1);
		int f=0;
		for(int i=2;i<=l;++i){
			while(f&&s[i]!=s[f+1]) f=ne[f];
			if(s[i]==s[f+1]) f++;
			ne[i]=f;
			if(i!=l)
			ma=max(ma,ne[i]);
		}
		if(l%(l-ne[l])==0){
			cout<<l/(l-1-ne[l])<<endl;
		}else{
			cout<<1<<endl;
		}
		memset(ne,0,sizeof(ne));
	}

    return 0;
}

posted @ 2021-10-12 13:58  Simex  阅读(32)  评论(0编辑  收藏  举报