模式匹配---KMP算法

来源:互联网 发布:mac口红好用吗 编辑:程序博客网 时间:2024/04/28 05:56

//KMP算法的效率主要是去掉了指针回溯,借助于实效函数,可以高效的移动指针到下一个检查位置

#include <string>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

void fail(char *src, int *f);
int KmpFind(char *src, char *pat, int fail[]);

int main()
{
 char src[100], pat[100];
 int f[100],pos;
 cout<<"输入原字符串:"<<endl;
 cin.getline(src,100);
 cout<<"输入子串:"<<endl;
 cin.getline(pat,100);
 fail(src,f);
 pos = KmpFind(src,pat,f);
 if(pos == -1) cout<<src<<" 中没有 "<<pat<<" 子串 "<<endl;
 else cout<<src<<" 中子串 "<<pat<<" 起始位置为: "<<pos<<endl;
 getch();
 return 0;
}

void fail(char *src , int *f)//src为原字符串,f[]为失效函数
{
 int lengthP = strlen(src);
 f[0] = -1;
 for(int j = 1; j < lengthP; j++)
 {
  int i = f[j-1];
  while(*(src + j) != *(src + i + 1) && i >= 0) i = f[i];
  if(*(src +j) == *(src + i + 1)) f[j] = i + 1;
  else f[j] = -1;
 }
}


int KmpFind(char *src, char *pat, int fail[])//fail[]为失效函数
{
 int posP = 0, posT = 0;
 int lengthP = strlen(pat), lengthT = strlen(src);
 while(posP < lengthP && posT < lengthT)
 {
  if(pat[posP] == src[posT])
  {
   posP++;
   posT++;
  }
  else if(posP == 0)
  {
   posT++;
  }
  else posP = fail[posP - 1] + 1;
 }
 if(posP < lengthP)
  return -1;
 else return posT - lengthP + 1;
}


原创粉丝点击