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>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcout(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
if (x >= mod)
x -= mod;
}
const int base = 311;
int h[maxn],p[maxn];
string s;
int n;
int get(int l,int r)
{
return (h[r] - h[l - 1] * p[r - l + 1] + mod * mod) % mod;
}
void solve()
{
cin >> s;
int n = ss(s);
s = ' ' + s;
fo(i,1,n)
{
h[i] = (h[i - 1] * base + s[i] - 'a' + 1) % mod;
}
int l = 1,r = n;
int cnt = 0;
while(l <= r)
{
int posl = l,posr = r;
bool ok = 0;
while(posl < posr)
{
if(get(l,posl) == get(posr,r))
{
cnt += 2;
ok = 1;
l = posl + 1;
r = posr - 1;
break;
}
posl++;
posr--;
}
if(!ok)
{
cnt++;
break;
}
}
cout << cnt;en;
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
p[0] = 1;
fo(i,1,maxn - 1) p[i] = p[i - 1] * base % mod;
int T;
cin >> T;
while (T--)
solve();
}
Compilation message (stderr)
palindromic.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | main()
| ^~~~
palindromic.cpp: In function 'int main()':
palindromic.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(name ".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... |