zoukankan      html  css  js  c++  java
  • KMP算法

    #include "stdio.h"
    #include "stdlib.h"
    #include "iostream.h"
    #define TRUE  1
    #define FALSE  0
    #define OK  1
    #define ERROR  0
    #define INFEASLBLE  -1
    #define OVERFLOW  -2
    #define MAXSTRLEN  255      //用户可在255以内定义最大串长
    typedef unsigned char SString[MAXSTRLEN+1]; //0号单元存放串的长度
     
    // 求模式串T的next函数值并存入数组next
    void get_next(SString T,int next[])
    {
       int i=0,j=1;
       next[1]=0;
       while(i
        if(j==0||T[i]==T[j]){
            i++;
            j++;
            next[i]=j;
        }
        else
           j=next[j];
       }
    }
     
    //KMP算法
    // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
    int Index_KMP(SString S,SString T,int pos)
    {
         int i=pos,j=1;
          while(i
            if(j==0||S[i]==T[j]){
                i++;
                j++;
            }
            else
                j=next[j];
          }
           if(j>T[0])
            return i-T[0];
           else
            return 0;
    }
     
    ---------------------------------------------------------
    //如数组零号单元不存字符个数而用来存字符元素,则为
    void get_next(SString T,int next[])    //求next数组
    {
       int i=0,j=-1;
       next[0]=-1;
       m=srelen(S);
       while(i
        if(j==-1||T[i]==T[j]){
            i++;
            j++;
            next[i]=j;
        }
        else
           j=next[j];
       }
    }
     
    //KMP算法
    // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
    int Index_KMP(SString S,SString T,int pos)
    {
         int i=pos,j=0;
         m=srelen(S);
         n=srelen(T);
         while(i
            if(j==-1||S[i]==T[j]){
                i++;
                j++;
            }
            else
                j=next[j];
          }
           if(j>m)
            return i-m;
           else
            return -1;
    }
    -----------------------------------------------------------
    //主函数
    void main()
    {
        SString T,S;
        int i,j,n;
        char ch;
        int pos;
        scanf(“%d”,&n);    // 指定n对需进行模式匹配的字符串
        ch=getchar();
        for(j=1; j<=n; j++)
        {
            ch=getchar();
            for( i=1; i<=MAXSTRLEN&&(ch!=' '); i++)  // 录入主串
            {
                S[i]=ch;
                ch=getchar();
            }
            S[0]=i-1;    // S[0]用于存储主串中字符个数
            ch=getchar();
            for( i=1; i<=MAXSTRLEN&&(ch!=' '); i++)  // 录入模式串
            {
                T[i]=ch;
                ch=getchar();
            }
            T[0]=i-1;    // T[0]用于存储模式串中字符个数
            pos=Index_KMP( S, T,pos) ;    // 请填空
            printf("%d ",pos);
        }
    }
  • 相关阅读:
    【洛谷P3853】 [TJOI2007]路标设置
    【洛谷P1159】排行榜
    【洛谷P2921】[USACO08DEC]在农场万圣节
    【洛谷P1108】低价购买
    【洛谷P1363】幻象迷宫
    【题解】洛谷P2023 [AHOI2009] 维护序列(线段树)
    【数据结构】线段树的几种用法
    【题解】洛谷P1283 平板涂色(搜索+暴力)
    【题解】洛谷P1074 [NOIP2009TG] 靶形数独(DFS+剪枝)
    【题解】洛谷P1120 小木棍(搜索+剪枝+卡常)
  • 原文地址:https://www.cnblogs.com/yujon/p/5467590.html
Copyright © 2011-2022 走看看