Advertisement
STANAANDREY

simulare osepi MMXI

Mar 7th, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int NMAX = 5e5 + 3;
  4. int n, a[NMAX], pi[NMAX];
  5.  
  6. void read() {
  7.     cin >> n;
  8.     for (int i = 1; i <= n; i++) {
  9.         cin >> a[i];
  10.     }
  11. }
  12.  
  13. signed main() {
  14.     read();
  15.     for (int i = 2; i <= n; i++) {
  16.         int curr = pi[i - 1];
  17.         while (curr > 0 && a[curr + 1] != a[i]) {
  18.             curr = pi[curr];
  19.         }
  20.         if (a[curr + 1] == a[i]) {
  21.             curr++;
  22.         }
  23.         pi[i] = curr;
  24.     }
  25.     cout << n - pi[n] << endl;
  26.     return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement