poj2406 KMP

kmp简单题 找循环节。由于KMP的next[]数组,所以可以考虑最后一组的情况,及next[n]的值;n-next[n]的值表示一个循环节。

如果n%(n-next[n])!=0表明该循环不成立。不然就是直接得到。

#include<stdio.h>
#include<string.h>
#define maxn 1000010
int next[maxn];
char s[maxn];
void getnext()
{
    int j,k,len=strlen(s);
    j=0;
    k=-1;
    next[0]=-1;
    while(j<len)
    {
        if(k==-1||s[j]==s[k])
        {
            j++;
            k++;
            next[j]=k;
        }
        else
        {
            k=next[k];
        }
    }
}
void getans()
{
    int ans,flen;
    int n=strlen(s);
    flen=n-next[n];
    if(n%flen==0)
        printf("%d\n",n/flen);
    else printf("1\n");
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        if(s[0]==‘.‘)break;
        getnext();
        getans();
    }
}
时间: 2024-10-13 05:55:55

poj2406 KMP的相关文章

POJ2406 KMP算法

POJ2406 问题重述:给定字符串s0,记连续的k个s前后相连组成的s0s0...s0为s0^k.输入字符串S,求最大的k,使得S = s0^k. 问题分析: 1.采用kmp算法求出前缀函数 prefix[i] (i = 1,2,... n, n = length(S)). 2.假如n - prefix[n]能够整除n, 则S能表示为k = n / (n - prefix[n])个连续s0,且k此时取到最大值 3.否则, S不能表示为连续的s0的形式, k = 1. AC代码: 1 //Mem

poj2406 kmp 求最小循环字串

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ-2406(KMP+字符串压缩)

Power String POJ-2406 字符串压缩模板题,但是是求有多少个这样最短的子串可以组成s. #include<iostream> #include<cstring> #include<cstdio> #include<string> #include<algorithm> using namespace std; const int N=10000001; int pi[N]; void Pi(string s){ memset(p

poj(2406) kmp

题目链接:https://vjudge.net/problem/POJ-2406 kmp学习:https://blog.csdn.net/starstar1992/article/details/54913261/ Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abc

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

poj2406 Power Strings (KMP)

这题跟HDU 1358 Period (KMP)差不多,稍微修改代码就行了. 关于KMP的更多知识,请关注从头到尾彻底理解KMP(2014年8月4日版). #include<stdio.h> #include<string.h> int n,next[1000000]; char p[1000000]; void getnext() { int k=0,j=1; next[0]=-1;next[1]=0; while (j<n) { if (k==-1||p[j]==p[k]

KMP算法及KMP算法的应用(POJ2406)

///KMP算法#include<bits/stdc++.h> using namespace std; int Next[1000]; void makeNext(const char P[],int next[]) { int q,k; int len=strlen(P); next[0]=0; for(q=1,k=0;q<len;q++) { while(k>0&&P[q]!=P[k]) { k=next[k-1]; } if(P[q]==P[k]) { k+

Power Strings(POJ2406)(KMP)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33623   Accepted: 13966 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

poj2406 Power Strings(kmp失配函数)

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcd