#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const double PI = 4 * atan(1);
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const ll MOD = 1e9 +7;
const ll BASE = 131;
long long modInverse(long long b){
long long ex = MOD - 2;
if (b==1){
return 1;
}
long long r = 1;
while (ex ){
if (ex&1){
r=(r * b)%MOD;
}
ex = ex >> 1;
b = (b * b)%MOD;
}
return r;
}
struct hsh {
vector<ll> pre;
int sz;
ll INV;
vector<ll> inv;
vector<ll> po;
void init(string s){
sz = sz(s);
pre.resize(sz);
inv.resize(sz + 5);
po.resize(sz + 5);
INV = modInverse(BASE);
f0r(i, sz(inv)){
if(i == 0) inv[i] = 1;
else inv[i] = (inv[i-1]*INV)%MOD;
}
f0r(i, sz(po)){
if(i == 0) po[i] = 1;
else po[i] = (po[i-1]*BASE)%MOD;
}
f0r(i, sz){
if(i == 0) pre[i] = (s[i] - 'a')*po[i];
else pre[i] = pre[i-1] + (s[i] - 'a')*po[i];
}
}
ll get(int l, int r){
ll ret = (pre[r] - (l == 0? 0: pre[l-1]))%MOD;
ret *= inv[l];
ret %= MOD;
return ret;
}
};
int solve(string s){
int n = sz(s);
int lo = 0;
int hi = n-1;
int l = lo;
int r = hi;
int ret = 0;
hsh h;
h.init(s);
//bonobo
while(true){
if(lo>hi) break;
if(l >= r){
ret++;
break;
}
if(h.get(lo, l) == h.get(r, hi)){
ret += 2;
lo = l;
hi = r;
lo++;
hi--;
l = lo;
r = hi;
}
else{
l++;
r--;
}
}
return ret;
}
int main(){
fast_io();
int t;
cin >> t;
string temporary;
getline(cin, temporary);
f0r(i, t){
string s;
getline(cin, s);
cout << solve(s) << endl;
}
return 0;
}
Compilation message
palindromic.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
palindromic.cpp: In function 'void io(std::__cxx11::string)':
palindromic.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FIN, "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~
palindromic.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FOUT, "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
788 KB |
Output is correct |
11 |
Correct |
4 ms |
644 KB |
Output is correct |
12 |
Correct |
6 ms |
736 KB |
Output is correct |
13 |
Correct |
6 ms |
728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
788 KB |
Output is correct |
11 |
Correct |
4 ms |
644 KB |
Output is correct |
12 |
Correct |
6 ms |
736 KB |
Output is correct |
13 |
Correct |
6 ms |
728 KB |
Output is correct |
14 |
Correct |
430 ms |
36724 KB |
Output is correct |
15 |
Correct |
240 ms |
31600 KB |
Output is correct |
16 |
Correct |
437 ms |
34656 KB |
Output is correct |
17 |
Correct |
214 ms |
19796 KB |
Output is correct |