Power Strings POJ - 2406(kmp, 循环节)

    科技2025-10-29  9

    Power Strings POJ - 2406

    题意:

    给你一个串,要求你划分成由 k个循环节组成的串。 输出: 字符串由几个循环节构成。

    思路:

    就是普通循环节。 求法的传送门

    反思:

    这一道题,刚开始做的时候,以为题目是给的循环串。(结果看错题目了,一直wa。qaq)1million是1e6.

    AC

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxm=1e6+10; char p[maxm]; int nxt[maxm]; void getnxt(char *p){ int m=strlen(p); int i=0,j=-1; nxt[0]=-1; while(i<m){ if(j==-1||p[i]==p[j])nxt[++i]=++j; else j=nxt[j]; } } int main() { // ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); while(scanf("%s", p)){//cin>>p){ if(p[0]=='.')break; getnxt(p); int m=strlen(p); int ans=m/(m-nxt[m]); if(m%(m-nxt[m])==0)printf("%d\n", ans);//cout<<ans<<endl; else puts("1"); } return 0; }
    Processed: 0.009, SQL: 8