#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
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 |
1 |
Correct |
6 ms |
8784 KB |
Output is correct |
2 |
Correct |
7 ms |
8784 KB |
Output is correct |
3 |
Correct |
7 ms |
8784 KB |
Output is correct |
4 |
Correct |
7 ms |
8784 KB |
Output is correct |
5 |
Correct |
7 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8784 KB |
Output is correct |
2 |
Correct |
7 ms |
8784 KB |
Output is correct |
3 |
Correct |
7 ms |
8784 KB |
Output is correct |
4 |
Correct |
7 ms |
8784 KB |
Output is correct |
5 |
Correct |
7 ms |
8784 KB |
Output is correct |
6 |
Correct |
6 ms |
8784 KB |
Output is correct |
7 |
Correct |
6 ms |
8784 KB |
Output is correct |
8 |
Correct |
7 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8784 KB |
Output is correct |
2 |
Correct |
7 ms |
8784 KB |
Output is correct |
3 |
Correct |
7 ms |
8784 KB |
Output is correct |
4 |
Correct |
7 ms |
8784 KB |
Output is correct |
5 |
Correct |
7 ms |
8784 KB |
Output is correct |
6 |
Correct |
6 ms |
8784 KB |
Output is correct |
7 |
Correct |
6 ms |
8784 KB |
Output is correct |
8 |
Correct |
7 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
8784 KB |
Output is correct |
10 |
Correct |
8 ms |
8996 KB |
Output is correct |
11 |
Correct |
8 ms |
8784 KB |
Output is correct |
12 |
Correct |
7 ms |
8784 KB |
Output is correct |
13 |
Correct |
8 ms |
8868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8784 KB |
Output is correct |
2 |
Correct |
7 ms |
8784 KB |
Output is correct |
3 |
Correct |
7 ms |
8784 KB |
Output is correct |
4 |
Correct |
7 ms |
8784 KB |
Output is correct |
5 |
Correct |
7 ms |
8784 KB |
Output is correct |
6 |
Correct |
6 ms |
8784 KB |
Output is correct |
7 |
Correct |
6 ms |
8784 KB |
Output is correct |
8 |
Correct |
7 ms |
8784 KB |
Output is correct |
9 |
Correct |
6 ms |
8784 KB |
Output is correct |
10 |
Correct |
8 ms |
8996 KB |
Output is correct |
11 |
Correct |
8 ms |
8784 KB |
Output is correct |
12 |
Correct |
7 ms |
8784 KB |
Output is correct |
13 |
Correct |
8 ms |
8868 KB |
Output is correct |
14 |
Correct |
100 ms |
27780 KB |
Output is correct |
15 |
Correct |
61 ms |
23032 KB |
Output is correct |
16 |
Correct |
97 ms |
28800 KB |
Output is correct |
17 |
Correct |
55 ms |
18908 KB |
Output is correct |