#include<bits/stdc++.h>
using namespace std;
void expand(string str, int low, int high, auto &set)
{
// run till str[low.high] is a palindrome
while (low >= 0 && high < str.length()
&& str[low] == str[high])
{
// push all palindromes into the set
set.insert(str.substr(low, high - low + 1));
// expand in both directions
low--, high++;
}
}
int ans=0;
string a;
vector<int> rabin_karp(string const& s, string const& t) {
const int p = 31;
const int m = 1e9 + 9;
int S = s.size(), T = t.size();
vector<long long> p_pow(max(S, T));
p_pow[0] = 1;
for (int i = 1; i < (int)p_pow.size(); i++)
p_pow[i] = (p_pow[i-1] * p) % m;
vector<long long> h(T + 1, 0);
for (int i = 0; i < T; i++)
h[i+1] = (h[i] + (t[i] - 'a' + 1) * p_pow[i]) % m;
long long h_s = 0;
for (int i = 0; i < S; i++)
h_s = (h_s + (s[i] - 'a' + 1) * p_pow[i]) % m;
vector<int> occurences;
for (int i = 0; i + S - 1 < T; i++) {
long long cur_h = (h[i+S] + m - h[i]) % m;
if (cur_h == h_s * p_pow[i] % m)
occurences.push_back(i);
}
return occurences;
}
void allPalindromicSubstrings(string str)
{
unordered_set<string> set;
for (int i = 0; i < str.length(); i++)
{
expand(str, i, i, set);
expand(str, i, i + 1, set);
}
for (auto i : set)
ans=max(ans,(int)((rabin_karp(i,a).size())*i.size()));
}
int main()
{
cin>>a;
allPalindromicSubstrings(a);
cout<<ans;
}
Compilation message
palindrome.cpp: In function 'void allPalindromicSubstrings(std::__cxx11::string)':
palindrome.cpp:47:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < str.length(); i++)
~~^~~~~~~~~~~~~~
palindrome.cpp: In instantiation of 'void expand(std::__cxx11::string, int, int, auto:1&) [with auto:1 = std::unordered_set<std::__cxx11::basic_string<char> >; std::__cxx11::string = std::__cxx11::basic_string<char>]':
palindrome.cpp:49:26: required from here
palindrome.cpp:6:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (low >= 0 && high < str.length()
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
252 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
296 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
420 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
3 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
380 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
296 KB |
Output is correct |
30 |
Correct |
2 ms |
256 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
956 KB |
Output is correct |
2 |
Correct |
45 ms |
672 KB |
Output is correct |
3 |
Correct |
166 ms |
948 KB |
Output is correct |
4 |
Correct |
23 ms |
404 KB |
Output is correct |
5 |
Correct |
166 ms |
944 KB |
Output is correct |
6 |
Correct |
167 ms |
928 KB |
Output is correct |
7 |
Correct |
21 ms |
796 KB |
Output is correct |
8 |
Correct |
92 ms |
888 KB |
Output is correct |
9 |
Correct |
18 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Correct |
4 ms |
400 KB |
Output is correct |
12 |
Correct |
17 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1050 ms |
7352 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1064 ms |
7584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1073 ms |
7760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |