Advertisement
minimario

<Z Algorithm> TEMPLATE

Oct 24th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 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 piii pair<pii, int>
  9. #define vi vector<int>
  10. #define vpii vector<pii>
  11.  
  12. #define read1(a) int a; scanf("%d", &a)
  13. #define read2(a, b) int a, b; scanf("%d %d", &a, &b)
  14. #define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)
  15.  
  16. #define FOR(i, a, b) for (int i=a; i<b; i++)
  17. #define F0R(i, a) for (int i=0; i<a; i++)
  18.  
  19. #define readgi(n) F0R(i, n) { scanf("%d", &arr[i]); }
  20. #define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }
  21.  
  22. #define f first
  23. #define s second
  24.  
  25. #define usaco(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout);
  26.  
  27. #define println1(a) printf("%d\n", a);
  28. #define println2(a, b) printf("%d %d\n", a, b);
  29. #define println3(a, b, c) printf("%d %d %d\n", a, b, c);
  30. #define pv(v) for (int i : v) { printf("%d ", i); } printf("\n");
  31.  
  32. const int MOD = 1000000007;
  33. const int MAX = 100005;
  34.  
  35. int zres[MAX];
  36.  
  37. void z(string s) {
  38.     int n = s.length();
  39.     int l = -1; int r = -1;
  40.     F0R(i, MAX) { zres[i] = 0; }
  41.     FOR(i, 1, n+1) {
  42.         if (i <= r) {
  43.             zres[i] = min(r - i + 1, zres[i-l]);
  44.         }
  45.         while (i + zres[i] < n && s[i+zres[i]] == s[zres[i]]) {
  46.             zres[i]++;
  47.         }
  48.         if (i + zres[i] - 1 > r) {
  49.             l = i;
  50.             r = i + zres[i] - 1;
  51.         }
  52.     }
  53. }
  54.  
  55. int main()
  56. {
  57.     string str;
  58.     cin >> str;
  59.     int l = str.length();
  60.     z(str);
  61.     zres[0] = l;
  62.     F0R(i, l) { cout << zres[i] << " "; }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement