Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Слава Україні, Героям слава
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2e5 + 7, alph = 26, L = 20;
- char s[N];
- int len[N], link[N], to[alph][N], par[N], sze[N], nxt[N], up[L][N];
- int n, last, sz, ans;
- void init() {
- memset(len, 63, sizeof(len));
- memset(nxt, -1, sizeof(nxt));
- s[n++] = -1;
- link[0] = 1;
- len[0] = 0;
- len[1] = -1;
- sz = 2;
- for(int i = 0; i < N; i++) {
- par[i] = i;
- sze[i] = 1;
- }
- }
- int find(int a) {
- return par[a] == a ? a : par[a] = find(par[a]);
- }
- bool unite(int a, int b) {
- if((a = find(a)) == (b = find(b))) return false;
- if(sze[a] < sze[b]) swap(a, b);
- sze[a] += sze[b];
- par[b] = a;
- return true;
- }
- int get_link(int v) {
- while(s[n - len[v] - 2] != s[n - 1]) v = link[v];
- return v;
- }
- void add_letter(char c, int ind) {
- s[n++] = c;
- last = get_link(last);
- if(!to[c - 'a'][last]) {
- len[sz] = len[last] + 2;
- link[sz] = to[c - 'a'][get_link(link[last])];
- to[c - 'a'][last] = sz++;
- last = to[c - 'a'][last];
- up[0][last] = link[last];
- for(int i = 1; i < L; i++) {
- up[i][last] = up[i - 1][up[i - 1][last]];
- }
- int cur = last;
- for(int i = L - 1; i >= 0; i--) {
- if(len[up[i][cur]] > (len[last] + 1) / 2) {
- cur = up[i][cur];
- }
- }
- nxt[last] = up[0][cur];
- } else {
- last = to[c - 'a'][last];
- }
- int cur = last;
- while(cur > 1) {
- ans -= unite(ind, ind - len[cur] + 1);
- cur = nxt[cur];
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- init();
- string str;
- cin >> str;
- ans = str.size();
- for(int i = 0; i < (int)str.size(); i++) {
- add_letter(str[i], i);
- }
- cout << ans << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement