zoukankan      html  css  js  c++  java
  • 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]!='')
        {
            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
    ",l);
        while(i <= l)
        {
        //    printf("i=%d j=%d
    ",i,j);
            if(s2[j] == '')
            {
                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
    ",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
    ");
        else
        {
            k = l%min;
            printf("%d
    ",min-k);
        }
        return;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        getchar();
        while(t--)
        {
            scanf("%s",s);
            GetNext(s);
        }
        return 0;
    }
  • 相关阅读:
    窗体控件JFrame的使用
    WindowBuilder的安装与简介
    Swing事件机制
    Swing的MVC结构
    Swing框架的继承关系
    SWT简介
    Swing简介
    AWT简介
    Java界面设计
    使用Java建立聊天客户端
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7770925.html
Copyright © 2011-2022 走看看