Advertisement
Kaidul

F. Scooby ‘Dooby’ Doo

Dec 25th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. #define Max 1000005
  7. #define PatM 1005
  8.  
  9. queue<int> result;
  10. char txt[Max], pat[PatM];
  11.  
  12. void computeLPSArray(int M, int *lps) {
  13.     int len = 0;  // lenght of the previous longest prefix suffix
  14.     int i;
  15.  
  16.     lps[0] = 0; // lps[0] is always 0
  17.     i = 1;
  18.  
  19.     // the loop calculates lps[i] for i = 1 to M-1
  20.     while(i < M) {
  21.         if(pat[i] == pat[len]) {
  22.             len++;
  23.             lps[i] = len;
  24.             i++;
  25.         } else { // (pat[i] != pat[len])
  26.             if( len != 0 ) {
  27.                 // This is tricky. Consider the example AAACAAAA and i = 7.
  28.                 len = lps[len-1];
  29.  
  30.                 // Also, note that we do not increment i here
  31.             } else { // if (len == 0)
  32.                 lps[i] = 0;
  33.                 i++;
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. void KMPSearch() {
  40.     int M = strlen(pat);
  41.     int N = strlen(txt);
  42.  
  43.     // create lps[] that will hold the longest prefix suffix values for pattern
  44.     int *lps = (int *)malloc(sizeof(int)*M);
  45.     int j  = 0;  // index for pat[]
  46.  
  47.     // Preprocess the pattern (calculate lps[] array)
  48.     computeLPSArray(M, lps);
  49.  
  50.     int i = 0;  // index for txt[]
  51.     while(i < N) {
  52.         if(pat[j] == txt[i]) {
  53.             j++;
  54.             i++;
  55.         }
  56.  
  57.         if (j == M) {
  58.             result.push(i - j);
  59.             j = lps[j-1];
  60.         }
  61.  
  62.         // mismatch after j matches
  63.         else if(pat[j] != txt[i]) {
  64.             // Do not match lps[0..lps[j-1]] characters,
  65.             // they will match anyway
  66.             if(j != 0)
  67.                 j = lps[j-1];
  68.             else
  69.                 i = i+1;
  70.         }
  71.     }
  72.     free(lps); // to avoid memory leak
  73. }
  74.  
  75. // Driver program to test above function
  76. int main() {
  77. #ifndef ONLINE_JUDGE
  78.     freopen("input.txt", "r", stdin);
  79. #endif
  80.     int tcase, caseNo = 0, front;
  81.     scanf("%d", &tcase);
  82.     getc(stdin);
  83.     while(tcase--) {
  84.         gets(txt);
  85.         gets(pat);
  86.         KMPSearch();
  87.         printf("Case %d: %d\n", ++caseNo, result.size());
  88.         while(!result.empty()) {
  89.             front = result.front();
  90.             result.pop();
  91.             printf("%d", front);
  92.             if(result.size()) printf(" ");
  93.         }
  94.         putchar('\n');
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement