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 sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define repd(i, r, l) for(int i = (r) - 1; i >= (l); --i)
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define FORD(i, r, l) for(int i = (r); i >= (l); --i)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
} return false;
}
template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
static_assert(dimension > 0, "Dimension must be positive !\n");
template<typename... Args>
tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
template<typename T>
struct tensor<1, T> : public vector<T> {
tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};
const int MAX = 1e6 + 5;
const int MOD[2] = {(int)1e9 + 9, (int)1e9 + 7};
const int BASE[2] = {31, 37};
int add(int a, int b, const int& mod){
return a + b < mod ? a + b : a + b - mod;
}
int subtract(int a, int b, const int& mod){
return a < b ? a - b + mod : a - b;
}
int multiply(int a, int b, const int& mod){
return 1LL * a * b % mod;
}
int n, pw[2][MAX], h[2][MAX];
string s;
int code(char c){
return c - 'a' + 1;
}
void init(){
for(int i = 1; i <= n; ++i){
for(int j : {0, 1}){
h[j][i] = add(multiply(h[j][i - 1], BASE[j], MOD[j]), code(s[i]), MOD[j]);
}
}
}
int getHash(int type, int l, int r){
return subtract(h[type][r], multiply(h[type][l - 1], pw[type][r - l + 1], MOD[type]), MOD[type]);
}
bool same(int l1, int r1, int l2, int r2){
return getHash(0, l1, r1) == getHash(0, l2, r2) && getHash(1, l1, r1) == getHash(1, l2, r2);
}
int solve(int l, int r){
if(l > r) return 0;
if(l == r) return 1;
int k = r - l + 1;
for(int i = 1; i < k; ++i){
if(same(l, l + i - 1, r - i + 1, r)){
return solve(l + i, r - i) + 2;
}
}
return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("task.inp", "r")){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
pw[0][0] = 1;
pw[1][0] = 1;
FOR(i, 1, 1000000){
pw[0][i] = multiply(pw[0][i - 1], BASE[0], MOD[0]);
pw[1][i] = multiply(pw[1][i - 1], BASE[1], MOD[1]);
}
int t;
cin >> t;
while(t--){
cin >> s;
n = sz(s);
s = '?' + s;
init();
cout << solve(1, n) << '\n';
}
return 0;
}
Compilation message (stderr)
palindromic.cpp: In function 'int main()':
palindromic.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |