#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);
cin >> A;
cout << Manacher(A);
}
Compilation message
palindrome.cpp: In function 'int zF(const string&)':
palindrome.cpp:24:22: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | ans += (z[i] == str.size());
palindrome.cpp: In function 'long long int Manacher(const string&)':
palindrome.cpp:57:9: warning: unused variable 'p' [-Wunused-variable]
57 | int p = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
412 KB |
Output is correct |
26 |
Correct |
1 ms |
424 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
852 KB |
Output is correct |
2 |
Correct |
49 ms |
616 KB |
Output is correct |
3 |
Correct |
703 ms |
1296 KB |
Output is correct |
4 |
Correct |
10 ms |
348 KB |
Output is correct |
5 |
Correct |
703 ms |
1512 KB |
Output is correct |
6 |
Correct |
727 ms |
1108 KB |
Output is correct |
7 |
Correct |
7 ms |
348 KB |
Output is correct |
8 |
Correct |
181 ms |
860 KB |
Output is correct |
9 |
Correct |
8 ms |
348 KB |
Output is correct |
10 |
Correct |
7 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
476 KB |
Output is correct |
12 |
Correct |
8 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1050 ms |
1120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
2196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1057 ms |
5636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |