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;
const int MXN = 1000006, SGM = 26;
int n, ts, link[MXN], len[MXN];
int slink[MXN], diff[MXN], sdp[MXN], dp[MXN];
map < int , int > C[MXN];
char S[MXN], _S[MXN];
inline int GetLink(int nw, int i)
{
while (S[i] != S[i - len[nw] - 1])
nw = link[nw];
return (nw);
}
inline int Add(int nw, int i)
{
int ch = S[i] - 'a';
nw = GetLink(nw, i);
if (!C[nw][ch])
{
ts ++;
len[ts] = len[nw] + 2;
link[ts] = C[GetLink(link[nw], i)][ch];
diff[ts] = len[ts] - len[link[ts]];
slink[ts] = link[ts];
if (diff[ts] == diff[link[ts]])
slink[ts] = slink[link[ts]];
C[nw][ch] = ts;
return (ts);
}
return (C[nw][ch]);
}
int main()
{
int q;
scanf("%d", &q);
for (; q; q --)
{
memset(_S, 0, sizeof(_S));
scanf("%s", _S);
n = strlen(_S);
for (int i = 1; i < n; i += 2)
{
S[i - 1] = _S[i >> 1];
S[i] = _S[n - 1 - (i >> 1)];
}
link[0] = link[1] = 1;
len[1] = -1; ts = 1;
sdp[0] = sdp[1] = - MXN;
dp[0] = 0;
int nw = 1, Mx = 1;
for (int i = 1; i <= (n >> 1 << 1); i++)
{
dp[i] = - MXN;
nw = Add(nw, i - 1);
for (int u = nw; len[u] > 0; u = slink[u])
{
sdp[u] = dp[i - len[slink[u]] - diff[u]];
if (diff[u] == diff[link[u]])
sdp[u] = max(sdp[u], sdp[link[u]]);
if (!(i & 1)) dp[i] = max(dp[i], sdp[u] + 1);
}
Mx = max(Mx, dp[i] + dp[i] + (i < n));
}
for (int i = 0; i <= ts; i++)
C[i].clear();
printf("%d\n", Mx);
}
return 0;
}
Compilation message (stderr)
palindromic.cpp: In function 'int main()':
palindromic.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
palindromic.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", _S);
~~~~~^~~~~~~~~~
# | 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... |