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

KMP

时间:2018-11-12 21:21:16      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:nbsp   clu   end   cstring   out   ++   下标   cin   const   

http://hihocoder.com/problemset/problem/1015

 

AC:

 1 #include<iostream>  
 2 #include<cstring> 
 3 #include<string>
 4 using namespace std;  
 5 int Next[10001];
 6 //求KMP的Next数组  
 7 void GetNext(const string& p)  
 8 {  
 9     int len=p.size();  
10     int i=0;  
11     int j=-1;  
12     Next[i]=j;  
13     while(i<len)  
14     {  
15         if(j==-1||p.at(i)==p.at(j))  
16         {  
17             i++;  
18             j++;  
19             if(i==len||p.at(i)!=p.at(j))//不允许出现P[i]==P[next[i]]  
20                 Next[i]=j;  
21             else  
22                 Next[i]=Next[j];  
23         }  
24         else  
25         {  
26             j=Next[j];  
27         }  
28     }  
29       
30 } 
31  
32  
33 //S为母串,p为匹配子串,如果匹配返回匹配位置,否则返回-1  
34 int KMPSearch(const string& s,const string& p)  
35 {  
36     unsigned int Slen=s.size();  
37     unsigned int Plen=p.size();
38     //int *Next=new int[Plen];//Next数组存储位置  
39   
40     GetNext(p);//求得Next数组  
41   
42     unsigned int i=0;//在S串中的下标  
43     unsigned int j=0;//在P串中的下标  
44     unsigned int count=0;//匹配串出现的次数
45     while(i<Slen)  
46     {  
47         if(j==-1||s.at(i)==p.at(j))  
48         {  
49             i++;  
50             j++;  
51         }  
52         else  if(j<Plen)
53             j=Next[j];  
54         if(j==Plen)
55         {
56             count++;
57             j=Next[Plen];
58         }
59     }   
60     return count;
61 }
62  
63 int main()  
64 {  
65     //freopen("in.txt", "r", stdin);
66     string str1;  
67     string str2;  
68     int n;
69     cin>>n;
70     for(int i=0;i<n;i++)
71     {
72         cin>>str2;
73         cin>>str1; 
74          
75         int pos=KMPSearch(str1,str2);  
76         cout<<pos<<endl; 
77     
78     }
79    
80     return 0;  
81 }  

WA:自己写的,本地样例全过,不知道错那

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 char T[1000000+5];
 5 char V[1000000+5];
 6  void getnext(char *T,int next[])     
 7  {
 8      int j=0;
 9      next[0]=0;
10      for(int i=1;i<strlen(T);i++)
11      {
12          while(j>0 && T[i]!=T[j]){
13              j=next[j-1];
14      }
15     if(T[i]==T[j]) j++;
16     next[i]=j;    
17  }
18  for(int i=strlen(T)-1;i>0;i--){     
19      next[i]=next[i-1];
20  }
21  next[0]=-1;
22  
23  }
24 
25 int kmp(char *T,char *V){             
26      int next[1000000+5];
27     int flag=0;
28      getnext(V,next);
29      int i=0,j=0;
30      while(i<strlen(T) && j< strlen(V)){
31          if(T[i]==V[j] ){
32              i++;
33              j++;
34          }
35          else{
36              
37              j=next[j];
38              if(j==-1){
39                  i++;j++;
40              }
41          }
42          
43          if(j==strlen(V)-1 && V[j]==T[i]){
44              flag++; 
45              j=next[j];
46          }
47      }
48      return flag;
49  }
50 
51 int main(int argc, char *argv[]) {
52     int n;
53     cin>>n;
54     while(n--){
55         scanf("%s%s",V,T);
56         if(n==0)
57         cout<<kmp(T,V)<<endl;
58         else
59         cout<<kmp(T,V);
60     }
61 }

 

KMP

标签:nbsp   clu   end   cstring   out   ++   下标   cin   const   

原文地址:https://www.cnblogs.com/wuruofeng/p/9949039.html

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