Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int pls(int a, int b, int MOD) {
- a += b;
- if (a >= MOD) a -= MOD;
- return a;
- }
- int mns(int a, int b, int MOD) {
- a -= b;
- if (a < 0) a += MOD;
- return a;
- }
- int prd(int a, int b, int MOD) {
- return 1ll * a * b % MOD;
- }
- const pair<int, int> mods = {1e9 + 7, 998244353}, bases = {179, 239};
- pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
- return make_pair(
- pls(a.first, b.first, mods.first),
- pls(a.second, b.second, mods.second)
- );
- }
- pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
- return make_pair(
- mns(a.first, b.first, mods.first),
- mns(a.second, b.second, mods.second)
- );
- }
- pair<int, int> operator*(pair<int, int> a, pair<int, int> b) {
- return make_pair(
- prd(a.first, b.first, mods.first),
- prd(a.second, b.second, mods.second)
- );
- }
- struct MyHash {
- vector<pair<int, int>> pref, pw;
- string s;
- int n;
- MyHash(string &_s) {
- s = _s;
- n = (int) s.size();
- pref.resize(n + 1, {0, 0});
- pw.resize(n + 1, {1, 1});
- for (int i = 1; i <= n; i++) {
- auto val = (int)(s[i - 1] - 'A' + 1);
- pref[i] = pref[i - 1] * bases + make_pair(val, val);
- pw[i] = pw[i - 1] * bases;
- }
- }
- pair<int, int> ask(int l, int r) {
- return pref[r] - pref[l] * pw[r - l];
- }
- };
- int hashesSolution(string &s) {
- string rev = s;
- reverse(rev.begin(), rev.end());
- MyHash HashS(s), HashRev(rev);
- int cnt = 0, N = (int) s.size();
- for (int len = 0; len <= N; len++) {
- if (HashS.ask(N - len, N) == HashS.ask(0, len) &&
- HashS.ask(len, N) == HashRev.ask(0, N - len)) {
- cnt++;
- }
- }
- return cnt;
- }
- int stupidSolution(string &s) {
- int cnt = 0;
- int N = (int) s.size();
- string rev = s;
- reverse(rev.begin(), rev.end());
- for (int len = 0; len <= N; len++) {
- string t;
- for (int i = N - len; i < N; i++) t += s[i];
- t += rev;
- bool fl = true;
- for (int i = 0; i < (int)t.size() / 2; i++) {
- if (t[i] != t[(int)t.size() - i - 1]) {
- fl = false;
- break;
- }
- }
- cnt += (int) fl;
- }
- return cnt;
- }
- mt19937 rnd(time(nullptr));
- string getRandomString(int len) {
- string res;
- for (int i = 0; i < len; i++) {
- int val = rnd();
- val = abs(val) % 52;
- if (val < 26) res += (char)(val + 'A');
- else res += (char)(val - 26 + 'a');
- }
- return res;
- }
- string correct(string &s) {
- string t;
- if (s.empty()) return "#";
- t += s[0];
- for (int i = 1; i < (int) s.size(); i++) {
- t += '#';
- t += s[i];
- }
- return t;
- }
- vector<int> manacher(string &s) {
- int n = (int) s.size();
- vector<int> d(n, 1);
- int l = 0, r = 0;
- for (int i = 1; i < n; i++) {
- if (i < r)
- d[i] = min(r - i + 1, d[l + r - i]);
- while (i - d[i] >= 0 && i + d[i] < n && s[i - d[i]] == s[i + d[i]])
- d[i]++;
- if (i + d[i] - 1 > r)
- l = i - d[i] + 1, r = i + d[i] - 1;
- }
- return d;
- }
- int manacherSolution(string &s) {
- string rev = s;
- reverse(rev.begin(), rev.end());
- string t = rev + s;
- t = correct(t);
- auto d = manacher(t);
- int cnt = 0, n = (int) t.size();
- for (int i = n / 2; i < n; i++) {
- if (i + d[i] == n && i - d[i] <= n / 2) {
- cnt++;
- }
- }
- return cnt;
- }
- void Fast() {
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- }
- signed main() {
- Fast();
- string s;
- cin >> s;
- cout << manacherSolution(s);
- // for (int len = 0; len <= 500; len++) {
- // for (int i = 0; i < 100; i++) {
- // string s = getRandomString(len);
- // int answer = hashesSolution(s), result = manacherSolution(s);
- // if (answer != result) {
- // cout << s << "\n" << result << " " << answer << endl;
- // }
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement