Advertisement
Guest User

second_pattern.cpp

a guest
Jan 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int fun(int sk,char tk,int TF[][256]);
  5. enum state{Q0,Q1,Q2,Q3,Q4,Q5,P};
  6. int main(){
  7.     char *txt="ccccaabaaa";
  8.     char *pat="aabaaa";
  9.     int num_of_char=2;
  10.     int PL=strlen(pat);
  11.     int N=strlen(txt);
  12.     int TF[PL][256];
  13.  
  14.     for(int i=0;i<PL;i++){
  15.         for(int j=0;j<=num_of_char;j++){
  16.             if(i==0 and j==0){
  17.                 TF[i][j]=Q1;
  18.             }else if(i==0 and j==1){
  19.                  TF[i][j]=Q0;
  20.             }//
  21.             else if(i==1 and j==0){
  22.                 TF[i][j]=Q2;
  23.             }else if(i==1 and j==1){
  24.                  TF[i][j]=Q0;
  25.             }//
  26.             else if(i==2 and j==0){
  27.                 TF[i][j]=Q2;
  28.             }else if(i==2 and j==1){
  29.                  TF[i][j]=Q3;
  30.             }//
  31.             else if(i==3 and j==0){
  32.                 TF[i][j]=Q4;
  33.             }else if(i==3 and j==1){
  34.                  TF[i][j]=Q0;
  35.             }//
  36.             else if(i==4 and j==0){
  37.                 TF[i][j]=Q5;
  38.             }else if(i==4 and j==1){
  39.                  TF[i][j]=Q0;
  40.             }//
  41.             else if(i==5 and j==0){
  42.                 TF[i][j]=P;
  43.             }else if(i==5 and j==1){
  44.                  TF[i][j]=Q3;
  45.             }
  46.             else{
  47.                 TF[i][j]=Q0;
  48.             }
  49.         }
  50.     }
  51.  
  52.  
  53.     int k=1,sk=Q0,i=0;
  54.  
  55.     //cout<<tk<<endl;
  56.     while(sk!=P and k<=N){
  57.         char tk=txt[i];
  58.     i++;
  59.         sk=fun(sk,tk,TF);
  60.       k++;
  61.         if(sk==P){
  62.             cout<<"found at "<<k-PL<<endl;
  63.         }
  64.     }
  65. }
  66. int fun(int sk,char tk,int TF[][256]){
  67.     if(tk=='a'){
  68.         return TF[sk][0];
  69.     }else if(tk=='b'){
  70.         return TF[sk][1];
  71.     }else{
  72.         return 0;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement