# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
901624 | alexandru_onut | Palindromes (APIO14_palindrome) | C++14 | 2 ms | 348 KiB |
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;
string A;
int zF (const string &str)
{
string aux = str + "#" + A;
int N = aux.size();
vector <int> z (N , 0);
int L = 0 , R = 0 , ans = 0;
for(int i = 0; i < N; ++i)
{
if(i < R)
z[i] = min(z[i - L] , R - i);
while(i + z[i] < N && aux[z[i]] == aux[i + z[i]])
++z[i];
if(i + z[i] > R)
{
R = i + z[i];
L = i;
}
ans += (z[i] == str.size());
}
return ans;
}
long long Manacher (const string &str)
{
string S;
vector <int> dp;
S.push_back('#');
for(int i = 0; i < (int) str.size(); ++i)
{
S.push_back(str[i]);
S.push_back('#');
}
int N = S.size();
dp = vector <int> (N , 0);
int C = 0 , R = 0;
for(int i = 0; i < N; ++i)
{
int M = 2 * C - i;
if(i < R)
dp[i] = min(dp[M] , R - i);
int a = i + (dp[i] + 1);
int b = i - (dp[i] + 1);
while(b >= 0 && a < N && S[a] == S[b])
++dp[i] , ++a , --b;
if(i + dp[i] > R)
{
C = i;
R = i + dp[i];
}
}
int p = 0;
string ans;
unordered_map <string , bool> use;
long long rasp = 0;
for(int i = 1; i < (int) S.size(); ++i)
{
string ans;
for(int j = i - dp[i]; j <= i + dp[i]; ++j)
if(S[j] != '#')
ans.push_back(S[j]);
long long nrAp = zF (ans) * ans.size();
if(!use[ans])
rasp = max(rasp , 1LL * nrAp) , use[ans] = 1;
}
return rasp;
}
int main()
{
ios_base :: sync_with_stdio(false); cin.tie(nullptr);
freopen ("palindrome.in" , "r" , stdin);
freopen ("palindrome.out" , "w" , stdout);
cin >> A;
cout << Manacher(A);
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |