//ᴇᴀᴄʜ ᴘᴇʀꜱᴏɴ ᴡɪʟʟ ᴏɴʟʏ ʜᴀᴠᴇ ᴡʜᴀᴛ ᴛʜᴇʏ ᴇɴᴅᴇᴀᴠᴏᴜʀᴇᴅ ᴛᴏᴡᴀʀᴅꜱ [53:39]
//Author: Sazid Hasan
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dl;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define file(); freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define flush fflush(stdout);
#define spc " "
#ifdef ONLINE_JUDGE
#define debarr(array)
#define deb(x)
#define del
#define debug(...)
#else
#define debarr(array) for(int w = 0; w < array.size(); w++) cerr << #array << "-" << w << " = " << array[w] << endl;
#define deb(x) cerr << #x << " = " << x << endl;
#define del cerr << '\n';
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__);
template <class X, class Y>
ostream& operator<<(ostream& os, const pair<X, Y>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os, const Container& x) {
int i = 0, n = distance(x.begin(), x.end());
os << "{ ";
for (const auto& y : x)
os << y << (++i < n ? ", " : "");
return os << " }";
}
template <typename... Args>
void _debug(const char* names, Args&&... args) {
string_view s(names);
cerr << "{ ";
size_t i = 0, cnt = 0, n = sizeof...(args);
auto next = [&]() {
while (i < s.size() && (s[i] == ' ' || s[i] == ',')) ++i;
size_t st = i;
while (i < s.size() && s[i] != ',') ++i;
return s.substr(st, i - st);
};
((cerr << next() << ": " << args << (++cnt < n ? ", " : "")), ...);
cerr << " }" << '\n';
}
#endif
const double PI = acos(-1);
const int MOD = 1000000007;
const int inf = (2147483647);
const double EPS = 1e-6;
const int MAXN = 1e5+10;
class Hashing{
private:
vl hash; ll A, B; int n;
vl P;
public:
Hashing(int _n, ll _A, ll _B, string &s){
A = _A;
B = _B;
n = _n;
hash.resize(n);
P.resize(n+3, 1);
P[1] = A;
hash[0] = s[0];
for(int i = 1; i < n; i++){
hash[i] = (hash[i-1]*A+s[i])%B;
}
for(int i = 2; i <= n; i++) P[i] = (P[i-1]*A)%B;
}
ll str(int l, int r){
if(l==0) return hash[r];
return (hash[r]-((hash[l-1]*P[r-l+1])%B)+B)%B;
}
};
bool solve(){
string s; cin >> s;
int n = s.size();
int cnt = 0;
int l1 = 0, r1 = 0, l2 = n-1, r2 = n-1;
Hashing A(n, 37, MOD, s);
Hashing B(n, 23, MOD+2, s);
while(r1<l2){
if(A.str(l1, r1)==A.str(l2, r2) && B.str(l1, r1)==B.str(l2, r2)){
cnt += 2;
if(r1+1==l2){
cout << cnt << endl; return 0;
}
l1 = r1 = r1+1;
l2 = r2 = l2-1;
}else{
r1++, l2--;
}
}
cout << ++cnt << endl;
return 1;
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int testcase = 1;
cin >> testcase;
for(int tc = 1; tc <= testcase; tc++){
// cerr << "TESTCASE - " << tc << endl;
solve();
//cout << (solve() ? "YES" : "NO") << '\n';
cerr << endl;
}
return 0;
}
# | 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... |