扩展KMP学习

来源:互联网 发布:网络行为管理 编辑:程序博客网 时间:2024/06/07 03:08
  • 模板转自:
    http://blog.csdn.net/dyx404514/article/details/41831947 (侵删)(扩展KMP的详细介绍也在里面)

  • next数组计算

const int maxn=100010;   //字符串长度最大值  int next[maxn],ex[maxn]; //ex数组即为extend数组  void GETNEXT(char *str)  {      int i=0,j,po,len=strlen(str);      next[0]=len;//初始化next[0]      while(str[i]==str[i+1]&&i+1<len)//计算next[1]          i++;      next[1]=i;      po=1;//初始化po的位置      for(i=2;i<len;i++)      {          if(next[i-po]+i<next[po]+po)//第一种情况,可以直接得到next[i]的值              next[i]=next[i-po];          else//第二种情况,要继续匹配才能得到next[i]的值          {              j=next[po]+po-i;              if(j<0)   j=0;//如果i>po+next[po],则要从头开始匹配              while(i+j<len&&str[j]==str[j+i])//计算next[i]                  j++;              next[i]=j;              po=i;//更新po的位置          }      }  }
  • extend 数组
void EXKMP(char *s1,char *s2)  {      int i=0,j,po,len=strlen(s1),l2=strlen(s2);      GETNEXT(s2);//计算子串的next数组      while(s1[i]==s2[i]&&i<l2&&i<len)//计算ex[0]          i++;      ex[0]=i;      po=0;//初始化po的位置      for(i=1;i<len;i++)      {          if(next[i-po]+i<ex[po]+po)//第一种情况,直接可以得到ex[i]的值              ex[i]=next[i-po];          else//第二种情况,要继续匹配才能得到ex[i]的值          {              j=ex[po]+po-i;              if(j<0)  j=0;//如果i>ex[po]+po则要从头开始匹配              while(i+j<len&&j<l2&&s1[j+i]==s2[j])//计算ex[i]                  j++;              ex[i]=j;              po=i;//更新po的位置          }      }  }  
0 0
原创粉丝点击