STANAANDREY

simulare osepi MMXI

Mar 7th, 2021
538
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×