This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18 + 50;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct hashBasic {
const long long M = 1e9 + 7;
const long long B = uniform_int_distribution<long long>(0, M - 1)(rng);
vector<long long> mpow, hash;
long long mul(long long a, long long b) {
__int128 x = (__int128)a * b; return x % M;
return (a * b) % M;
}
hashBasic(string s) {
hash = vector<long long> (s.size() + 1);
mpow.push_back(1); hash[0] = 0;
while (mpow.size() < s.size()) {
mpow.push_back(mul(mpow.back(), B)); }
for (int i = 0; i < s.size(); ++i) {
hash[i+1] = (mul(hash[i], B) + s[i]) % M; }
}
long long get(int l, int r) { //on [l, r]
r++;
long long ret = hash[r] - mul(hash[l], mpow[r - l]);
return (ret + M) % M;
}
};
void solve() {
string s;
cin >> s;
int n = (int) s.size();
hashBasic h1(s), h2(s);
int ans = 0;
int l = 0, r = n - 1, l0 = -1, r0 = n;
while (l < r) {
if (h1.get(l0 + 1, l) == h1.get(r, r0 - 1) && h2.get(l0 + 1, l) == h2.get(r, r0 - 1)) {
ans += 2;
l0 = l;
r0 = r;
}
l++;
r--;
}
if (l0 < r0) ans++;
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message (stderr)
palindromic.cpp: In constructor 'hashBasic::hashBasic(std::string)':
palindromic.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < s.size(); ++i) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |