Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int NMAX = 5e5 + 3;
- int n, a[NMAX], pi[NMAX];
- void read() {
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- }
- signed main() {
- read();
- for (int i = 2; i <= n; i++) {
- int curr = pi[i - 1];
- while (curr > 0 && a[curr + 1] != a[i]) {
- curr = pi[curr];
- }
- if (a[curr + 1] == a[i]) {
- curr++;
- }
- pi[i] = curr;
- }
- cout << n - pi[n] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement