• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

KMP字符串匹配点算法模板!

KMP!递推求next位置!O(n+m)

 

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <queue>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 1000005
12 #define ll long long
13 #define INF 0x7fffffff
14 int n,m;
15 char s[maxn];
16 char p[maxn];
17 int f[maxn];
18 void getfail(char *p){
19     f[0]=f[1]=0;
20     int m=strlen(p);
21     for(int i=1;i<m;i++){
22         int j=f[i];
23         while(j&&p[i]!=p[j])j=f[j];
24         f[i+1]=p[i]==p[j]?j+1:0;
25     }
26 }
27 int KMP(char *s,char *p){
28     int n=strlen(s);
29     int m=strlen(p);
30     getfail(p);
31     int j=0;
32     for(int i=0;i<n;i++){
33         while(j&&s[i]!=p[j])j=f[j];
34         if(s[i]==s[j])j++;
35         if(j==m)return i-m+1;
36     }
37 }
38 int main(){
39     while(~scanf("%d",&n)){
40 
41     }
42     return 0;
43 }
View Code 2013-10-17 18:53:37 

 

posted @ 2013-10-17 18:52  HaibaraAi  阅读(82)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3