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;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
struct Hash{
vector <int> p, pw;
const int P = 31;
// 0 - indexed
string s;
int n, Mod;
Hash(int Mod) : Mod(Mod) {}
void ins(string _s){
s = _s;
n = (int)s.size();
p.resize(n + 1); pw.pb(1);
while ( (int)pw.size() <= n ){
pw.pb(pw.back() * P % Mod);
}
for ( int i = 0; i < n; i++ ){
int f = s[i] - 'a' + 1; // *appropriate subtraction
p[i + 1] = (p[i] * P + f) % Mod;
}
}
int get(int l, int r){
int ans = (p[r + 1] - p[l] * pw[r - l + 1]) % Mod;
if ( ans < 0 ) ans += Mod;
return ans;
}
};
void solve(){
string s; cin >> s;
Hash h(998244353);
h.ins(s);
int l = 0, r = (int)s.size() - 1, ans = 0;
while ( l <= r ){
if ( l == r ){
ans++;
break;
}
int a = l, b = r;
while ( a < b && h.get(l, a) != h.get(b, r) ){
a++, b--;
}
if ( a >= b ){
ans++;
break;
}
ans += 2;
l = a + 1, r = b - 1;
}
cout << ans << ln;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while ( t-- ){
solve();
}
cout << '\n';
}
# | 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... |