最長字串匹配演算法(KMP演算法)

來源:互聯網
上載者:User

#include "stdafx.h"

#include<iostream>

#include<time.h>

#include<string>

using namespace std;

void init(string ,string);

void show(char [],int);

int kmp(string ,string,int pos);

void get_next(char*,int *);

string s1,t1;

int m,n;

char *s;

char *t;

int *next;

/*************************MAIN**************************************/

int main(int argc[],char*args[])

{

   

 double t=clock();

 cout<<"起始時間:"<<t<<endl;

 cout<<"找到位置為:"<<kmp("acbsabcaacabaabaabcacaabc","abaabca",1)<<endl;

    delete[] s;

 delete[] next;

 cout<<"結束時間:"<<clock()<<endl;

 cout<<"耗時:"<<(clock()-t)/1000<<"毫秒!"<<endl;

 return 0;

}  

/**********************++++NEXT++++********************************/

void get_next(char s[],int ne[])

{

    ne =new int[n+1];

    next=new int[n+1];

    ne[0]=9999;

    int i(1),j(0);

    ne[1]=0;

    while(i<=(int)(t[0]))//數組是字元型的,要轉化為整數

 {

     if(j==0||t[i]==t[j]){++i;++j;ne[i]=j;}

     else j=ne[j];

 }

   for( i=1;i<=n;i++)

   {

   cout<<"next["<<i<<"]="<<ne[i]<<endl;

      next[i]=ne[i];

   }

}

/********************++++KMP+++**********************************/

  int kmp(string s0,string t0,int pos)

  {

        init(s0,t0);

        int i=pos,j=1;

        while(i<=((int)(s[0]))&&(j<=((int)(t[0]))))

  {

         if((j==0)||(s[i]==t[j])){++i;++j;}

      else j=next[j];

        

  }

        if(j>(int)(t[0])) return i-((int)(t[0]));

        else return 0;

  }

/***************++++INIT+++*****************************************/

    void init(string ss,string tt)

 {

       //cout<<"請輸入原串S=: "<<endl;

       //cin>>s1;

       //cout<<"請輸入模式串T=:"<<endl;

       //cin>>t1;

       s1=ss;

       t1=tt;

       m=s1.length();

       n=t1.length();

       //if((s=(char*)malloc((m+1)*sizeof(char)))<0){cout<<"failed\n";return;}

       s=new char[m+1];

       s[0]=m;

       //if((t=(char*) malloc((n+1)*sizeof(char)))<0) {cout<<"failed\n";return;}

       t=new char[n+1];

       t[0]=n;

       for(int i=1;i<=m;i++)

      s[i]=s1.at(i-1);

       for( i=1;i<=n;i++)

         t[i]=t1.at(i-1);

        cout<<"原串為: ";    show(s,m);

     cout<<"模式串為: ";  show(t,n);

        get_next(t,next);

 }

/*******************++++SHOW+++**************************************/

       void show(char s[],int n )

    {

         for(int i=1;i<=n;i++)

         cout<<s[i]<<"  ";

         cout<<endl;

         cout<<"長度為: "<<int(s[0])<<"\n";

    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.