zoukankan      html  css  js  c++  java
  • KMP

    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 }
  • 相关阅读:
    ubuntu16.04截图工具Shutter安装,设置快捷键
    cd命令和roscd命令的区别,并解决环境变量问题
    ubuntu16.04+kinetic 环境下进行turtlebot2安装+gazebo7.16
    【转载】windows 下TexLive的安装和配置
    Linux 环境变量 /etc/profile 和 ~/.bashrc的区别和使用
    rotors无人机仿真(古月居)——问题记录
    操作select
    #if DEBUG
    (网上看的)asp.net使用uploadify上传出现的IO Error问题
    sqlserver2008附加sqlserver2005数据库目录出错,需要设置mdf后缀文件夹“管理员取得所有权”,并用windows管理权限登录数据库不要用sa
  • 原文地址:https://www.cnblogs.com/wuruofeng/p/9949039.html
Copyright © 2011-2022 走看看