///breaker
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define ii pair<int,int>
const int maxn=3e5+7;
string s;
struct Node{
int nxt[26],sufflink;
ll len,cnt;
vector<int>edges;
}tree[maxn];
int suff,num;
ll ans=0;
void add(int pos){
int curr=suff,curr_len=0;
int letter=s[pos]-'a';
while(true){
curr_len=tree[curr].len;
if(pos-1-curr_len>=0&&s[pos-1-curr_len]==s[pos])break;
curr=tree[curr].sufflink;
}
if(tree[curr].nxt[letter]){
suff=tree[curr].nxt[letter];
tree[suff].cnt++;
return;
}
suff=++num;
tree[num].len=tree[curr].len+2;
tree[num].cnt=1;
tree[curr].nxt[letter]=num;
if(tree[num].len==1){
tree[num].sufflink=2;
tree[2].edges.push_back(num);
return;
}
while(true){
curr=tree[curr].sufflink;
curr_len=tree[curr].len;
if(pos-1-curr_len>=0&&s[pos-1-curr_len]==s[pos]){
tree[num].sufflink=tree[curr].nxt[letter];
tree[tree[curr].nxt[letter]].edges.push_back(num);
break;
}
}
}
void init(){
num=2,suff=2;
tree[1].len=-1;
tree[2].len=0;
tree[1].sufflink=1;
tree[2].sufflink=1;
tree[1].edges.push_back(2);
}
int dfs(int u=1)
{
for(int i:tree[u].edges){
tree[u].cnt+=dfs(i);
}
ans=max(ans,tree[u].cnt*tree[u].len);
return tree[u].cnt;
}
void sol(){
cin>>s;
init();
for(int i=0;i<s.size();i++)add(i);
dfs();
cout<<ans;
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t=1;
while(t--){
sol();
}
// you should actually read the stuff at the bottom
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
palindrome.cpp: In function 'void sol()':
palindrome.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0;i<s.size();i++)add(i);
| ~^~~~~~~~~
palindrome.cpp: At global scope:
palindrome.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
44892 KB |
Output is correct |
2 |
Correct |
16 ms |
44852 KB |
Output is correct |
3 |
Correct |
16 ms |
45016 KB |
Output is correct |
4 |
Correct |
16 ms |
44892 KB |
Output is correct |
5 |
Correct |
24 ms |
44892 KB |
Output is correct |
6 |
Correct |
17 ms |
44888 KB |
Output is correct |
7 |
Correct |
23 ms |
44892 KB |
Output is correct |
8 |
Correct |
17 ms |
44888 KB |
Output is correct |
9 |
Correct |
17 ms |
44888 KB |
Output is correct |
10 |
Correct |
15 ms |
44892 KB |
Output is correct |
11 |
Correct |
17 ms |
44864 KB |
Output is correct |
12 |
Correct |
17 ms |
44888 KB |
Output is correct |
13 |
Correct |
17 ms |
44892 KB |
Output is correct |
14 |
Correct |
15 ms |
44960 KB |
Output is correct |
15 |
Correct |
22 ms |
44892 KB |
Output is correct |
16 |
Correct |
23 ms |
44884 KB |
Output is correct |
17 |
Correct |
17 ms |
44888 KB |
Output is correct |
18 |
Correct |
17 ms |
44888 KB |
Output is correct |
19 |
Correct |
24 ms |
45056 KB |
Output is correct |
20 |
Correct |
23 ms |
44892 KB |
Output is correct |
21 |
Correct |
18 ms |
44888 KB |
Output is correct |
22 |
Correct |
18 ms |
44892 KB |
Output is correct |
23 |
Correct |
17 ms |
44892 KB |
Output is correct |
24 |
Correct |
17 ms |
44900 KB |
Output is correct |
25 |
Correct |
19 ms |
44892 KB |
Output is correct |
26 |
Correct |
17 ms |
44888 KB |
Output is correct |
27 |
Correct |
17 ms |
45072 KB |
Output is correct |
28 |
Correct |
18 ms |
44892 KB |
Output is correct |
29 |
Correct |
17 ms |
44892 KB |
Output is correct |
30 |
Correct |
16 ms |
45072 KB |
Output is correct |
31 |
Correct |
16 ms |
44892 KB |
Output is correct |
32 |
Correct |
16 ms |
44892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
45148 KB |
Output is correct |
2 |
Correct |
16 ms |
44892 KB |
Output is correct |
3 |
Correct |
16 ms |
45144 KB |
Output is correct |
4 |
Correct |
22 ms |
44892 KB |
Output is correct |
5 |
Correct |
18 ms |
45148 KB |
Output is correct |
6 |
Correct |
16 ms |
45144 KB |
Output is correct |
7 |
Correct |
17 ms |
44888 KB |
Output is correct |
8 |
Correct |
18 ms |
45144 KB |
Output is correct |
9 |
Correct |
17 ms |
44888 KB |
Output is correct |
10 |
Correct |
22 ms |
44880 KB |
Output is correct |
11 |
Correct |
22 ms |
44968 KB |
Output is correct |
12 |
Correct |
16 ms |
44892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
45400 KB |
Output is correct |
2 |
Correct |
24 ms |
45404 KB |
Output is correct |
3 |
Correct |
17 ms |
45676 KB |
Output is correct |
4 |
Correct |
16 ms |
45656 KB |
Output is correct |
5 |
Correct |
17 ms |
45144 KB |
Output is correct |
6 |
Correct |
19 ms |
45124 KB |
Output is correct |
7 |
Correct |
16 ms |
45404 KB |
Output is correct |
8 |
Correct |
17 ms |
45088 KB |
Output is correct |
9 |
Correct |
18 ms |
44892 KB |
Output is correct |
10 |
Correct |
16 ms |
45148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
50520 KB |
Output is correct |
2 |
Correct |
23 ms |
49500 KB |
Output is correct |
3 |
Correct |
26 ms |
53072 KB |
Output is correct |
4 |
Correct |
24 ms |
51292 KB |
Output is correct |
5 |
Correct |
32 ms |
46216 KB |
Output is correct |
6 |
Correct |
22 ms |
47136 KB |
Output is correct |
7 |
Correct |
22 ms |
48220 KB |
Output is correct |
8 |
Correct |
24 ms |
45292 KB |
Output is correct |
9 |
Correct |
21 ms |
46972 KB |
Output is correct |
10 |
Correct |
28 ms |
46164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
61920 KB |
Output is correct |
2 |
Correct |
36 ms |
57060 KB |
Output is correct |
3 |
Correct |
48 ms |
69096 KB |
Output is correct |
4 |
Correct |
41 ms |
59944 KB |
Output is correct |
5 |
Correct |
45 ms |
49880 KB |
Output is correct |
6 |
Correct |
37 ms |
55768 KB |
Output is correct |
7 |
Correct |
35 ms |
54028 KB |
Output is correct |
8 |
Correct |
21 ms |
45792 KB |
Output is correct |
9 |
Correct |
22 ms |
45848 KB |
Output is correct |
10 |
Correct |
39 ms |
49124 KB |
Output is correct |
11 |
Correct |
35 ms |
57308 KB |
Output is correct |
12 |
Correct |
23 ms |
47588 KB |
Output is correct |