。。。

导航

hdu2087剪花布条+hdu3746Cyclic Nacklace【kmp复习】

http://acm.hdu.edu.cn/showproblem.php?pid=2087

 

#include<stdio.h>
#include<string.h>
#define N 1010
int next[N],count ;
void GetNext(char *s)
{
    int  k,j;
    next[0] = k = -1;
    j = 0;
    while(s[j]!='\0')
    {
        if( k == -1||s[j]==s[k])
            next[++j] = ++k;
        else
            k = next[k];
    }
    return ;
}
void GetKmp(char *s1,char *s2)
{
    int i,j,l;
    i = j = 0;
    l = strlen(s1);
//    printf("l=%d\n",l);
    while(i <= l)
    {
    //    printf("i=%d j=%d\n",i,j);
        if(s2[j] == '\0')
        {
            j = 0;
            ++count;
        }
        else 
        {
            while(j!=-1&&s1[i]!=s2[j])
                j = next[j];
            ++i;
            ++j;
        }
    }
    return;
}
int main()
{
    char s1[N],s2[N];
    while(scanf("%s%s",s1,s2),s1[0]!='#')
    {
        count = 0;
        GetNext(s2);
        GetKmp(s1,s2);
        printf("%d\n",count);
    }
    return 0;
}

 http://acm.hdu.edu.cn/showproblem.php?pid=3746

 

#include<stdio.h>
#include<string.h>
#define N 100010
int next[N],l;
char s[N];

void GetNext(char *s)
{
    int i,j,k,l,min;
    j = i = 0;
    next[0] = k = -1;
    l = strlen(s);
    while(j<= l)
    {
        if(k == -1||s[k]==s[j])
            next[++j] = ++k;
        else
            k = next[k];
    }
    min = l-next[l];
    if(l%min==0&&l!=min)
        printf("0\n");
    else
    {
        k = l%min;
        printf("%d\n",min-k);
    }
    return;
}

int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        scanf("%s",s);
        GetNext(s);
    }
    return 0;
}

 

posted on 2017-11-02 10:14  大学僧  阅读(105)  评论(0编辑  收藏  举报