码迷,mamicode.com
首页 > 其他好文 > 详细

KMP模板

时间:2019-06-07 21:07:30      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:spl   pre   name   技术   std   ++   div   --   cst   

技术图片
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 const int N=1e6+5;
 8 char s[N],t[10005];
 9 
10 void strstr(){
11     int next[10005];
12     memset(next,0,sizeof(next));
13     next[0]=-1;
14     int j=0,k=-1;
15     while(j<strlen(t)){
16         if(k==-1||(t[j]==t[k])){
17             next[++j]=++k;
18         }
19         else k=next[k];
20     }
21     j=0,k=0;
22     int num=0;
23     while(j<strlen(s)){
24         if(k==-1||(s[j]==t[k])){
25             j++,k++;
26         }
27         else k=next[k];
28         if(k==strlen(t)) num++,k=next[k];
29     }
30     printf("%d\n",num);
31 }
32 
33 int main()
34 {
35     int n;
36     cin>>n;
37     while(n--){
38         cin>>t>>s;
39         strstr();
40     }
41 }
View Code

 

KMP模板

标签:spl   pre   name   技术   std   ++   div   --   cst   

原文地址:https://www.cnblogs.com/qq-1585047819/p/10989125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!