Advertisement
intricate_potato

KMP_Implementation

Feb 15th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> createLPS(string pattern);
  7. void kmp(string text, string pattern);
  8.  
  9. int main()
  10. {
  11.     int t;
  12.     cout<<"Enter Number of Test Case: ";
  13.     cin>>t; getchar();
  14.     while(t--)
  15.     {
  16.         string pattern, text;
  17.         getline(cin, text);
  18.         getline(cin, pattern);
  19.         kmp(text, pattern);
  20.     }
  21.    
  22.     return 0;
  23. }
  24.  
  25. //refer to Fig. 17 for visualization
  26. vector<int> createLPS(string pattern)
  27. {
  28.     vector<int> LPS(pattern.length());
  29.     int index = 0; //pointing to the beginning of pattern
  30.  
  31.     // Parsing the pattern
  32.        // index will always follow i and i will be incremented if there is a match
  33.     for(int i = 1; i < pattern.length();)
  34.     {
  35.         if(pattern[index] == pattern[i])
  36.         {
  37.             LPS[i] = index + 1; //because found match on i
  38.             index++; i++; //if matches, increase both index and i
  39.         }
  40.        
  41.         else
  42.         {
  43.             //if no match, decrease look at the previous LPS value and
  44.             //set it to the present index.
  45.             if(index != 0) //holding the index inside the array.
  46.                 index = LPS[index - 1];
  47.             else
  48.                 {   //index 0 means we can not decrease
  49.                                         //rather assign the 0 value to LPS[i]
  50.                     LPS[i] = index ;
  51.                     //since we can not match any more, we take i to one step right.
  52.                     i++;
  53.                 }
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     return LPS;
  60. }
  61.  
  62.  
  63. void kmp(string text, string pattern)
  64. {
  65.     //first the LPS table is needed as seen in the Example
  66.     vector<int> LPS = createLPS(pattern);
  67.     bool flag = false;
  68.     int i = 0, j = 0;
  69.     // i for text, j for pattern, recall in the example we said we can also
  70.     //return the position of occurrence in the text, i will help in this matter.
  71.  
  72.     //Parsing the text
  73.     while(i<text.length())
  74.     {
  75.         if(text[i]==pattern[j]) //recalling the green cells in the example
  76.         {
  77.             i++, j++; //advancing forward, can bee seen in Fig. 12
  78.         }
  79.        
  80.        
  81.         else //for mismatch, LPS table comes into the picture
  82.         {
  83.             if(j!=0)     // we need to consult the LPS table, refer to Fig 12 for this scenario
  84.                 j=LPS[j-1];
  85.             else       
  86.             //for j=0 in mismatch see the Fig. 11 and 15,
  87.             //we can not match anymore so we go right in the text.
  88.                 i++;
  89.         }
  90.  
  91.         //If we reach the end of the pattern then we have found the pattern
  92.         if (j==pattern.length()) //the last match will make j equal to pattern length
  93.         {
  94.             //cout << "OCCURANCE FOUND" << endl;
  95.             //adding 1 for convenience of the user
  96.             cout<<"POSITION OF OCCURRENCE: "<< i-pattern.length()+1<<endl;
  97.             flag=true;
  98.             j=LPS[j-1]; // we look for multiple occurrences
  99.         }
  100.  
  101.     }
  102.  
  103.     if(!flag) cout<<"NO OCCURRENCE FOUND"<<endl;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement