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

    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
     
    using namespace std;
     
    void makeNext(char s[],int next[])
    {
        int len = strlen(s);
        next[0]=0;
        for(int i=1,k=0;i<len;i++)
        {
            while(k>0 && s[k]!=s[i])
                k=next[k-1];
            if(s[k]==s[i])
                k++;
            next[i]=k;
        }
    }
     
    int kmp(char t[],char s[])
    {
        int len1 = strlen(t);
        int len2 = strlen(s);
        int next[len2];
        makeNext(s,next);
        for(int i=0,j=0;i<len1;i++)
        {
            while(j>0 && t[i]!=s[j])
            {
                j=next[j-1];
            }
            if(t[i]==s[j])
                j++;
            if(j==len2)
                return i-j+1;
        }
    }
     
    int main()
    {
        char t[]="1234561123458412";
        char s[]="611";
        cout<<t<<endl;
        cout<<s<<endl;
        cout<<"下标为"<<kmp(t,s)<<endl;
    }
    

      

    ————————————————
    版权声明:本文为CSDN博主「ExcesiveYue」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/yutong5818/article/details/81319120

  • 相关阅读:
    POJ 1680 Fork() Makes Trouble
    课堂改进意见
    梦断代码 读后感3
    梦断代码 读后感2
    找一问题
    软件评价——搜狗输入法
    《梦断代码》读后感1
    站立会议第十天
    站立会议第九天
    站立会议第八天
  • 原文地址:https://www.cnblogs.com/Cnxz/p/12456320.html
Copyright © 2011-2022 走看看