Advertisement
minimario

<KMP> TEMPLATE

Oct 24th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define mp make_pair
  6. #define pb push_back
  7. #define pii pair <int, int>
  8. #define vi vector<int>
  9.  
  10. #define read1(a) int a; scanf("%d", &a)
  11. #define read2(a, b) int a, b; scanf("%d %d", &a, &b)
  12. #define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)
  13.  
  14. #define FOR(i, a, b) for (int i=a; i<b; i++)
  15. #define F0R(i, a) for (int i=0; i<a; i++)
  16.  
  17. #define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
  18. #define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }
  19.  
  20. #define f first
  21. #define s second
  22.  
  23. const int MOD = 1000000007;
  24. const int MAX = 100005;
  25.  
  26. int b[MAX];
  27. string s;
  28.  
  29. void kmp()
  30. {
  31.     int i=0, j=-1;
  32.     b[i]=j;
  33.     while (i<s.length())
  34.     {
  35.         while (j>=0 && s[i]!=s[j]) j=b[j];
  36.         i++; j++;
  37.         b[i]=j;
  38.     }
  39. }
  40.  
  41. int main() {
  42.     s = "ababaa";
  43.     kmp();
  44.     F0R(i, 10) { cout << b[i] << " ";}
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement