#include<bits/stdc++.h>
using namespace std;
const int mx = 300005;
struct Node
{
int len,link,next[26];
};
string s;
Node tr[mx];
int node; /// node 1 - root with len -1, node 2 - root with len 0
int suff; /// max suffix palindrome
inline int New()
{
node++;
tr[node].len = 0, tr[node].link = 0;
memset(tr[node].next, 0, sizeof tr[node].next);
return node;
}
int cnt[mx];
inline bool Insert(int pos)
{
int cur = suff, curlen = 0;
int c = s[pos]-'a'; ///check it
while(true) ///Finding maximum length palindromic suffix
{
curlen = tr[cur].len;
if (pos-1-curlen>=0 && s[pos-1-curlen]==s[pos])
break;
cur = tr[cur].link;
}
if(tr[cur].next[c]) ///Existing node
{
suff = tr[cur].next[c];
cnt[suff]++;
return false;
}
suff = New();
tr[node].len = tr[cur].len + 2;
tr[cur].next[c] = node;
if(tr[node].len == 1) ///Single character, connected with root
{
tr[node].link = 2;
cnt[suff]++;
return true;
}
while(true)///Finding suffix link
{
cur = tr[cur].link;
curlen = tr[cur].len;
if(pos-1-curlen>=0 && s[pos-1-curlen]==s[pos])
{
tr[node].link = tr[cur].next[c];
break;
}
}
cnt[suff]++;
return true;
}
void initTree()
{
node = 2, suff = 2;
tr[1].len = -1, tr[1].link = 1;
tr[2].len = 0, tr[2].link = 1;
memset(tr[1].next,0,sizeof tr[1].next);
memset(tr[2].next,0,sizeof tr[2].next);
}
void buildTree()
{
initTree();
for(int i=0;i<s.size();i++)
{
Insert(i);
//cnt[tr[suff].link]+=cnt[suff];
}
}
long long sm = 0;
void count()
{
for(int i=node;i>2;i--)
{
cnt[tr[i].link]+=cnt[i];
sm = max(sm,1LL*cnt[i]*tr[i].len);
}
}
int main()
{
cin >> s;
buildTree();
count();
cout << sm << endl;
return 0;
}
Compilation message
palindrome.cpp: In function 'void buildTree()':
palindrome.cpp:72:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<s.size();i++)
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
380 KB |
Output is correct |
29 |
Correct |
2 ms |
380 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1528 KB |
Output is correct |
2 |
Correct |
4 ms |
1528 KB |
Output is correct |
3 |
Correct |
4 ms |
1500 KB |
Output is correct |
4 |
Correct |
4 ms |
1528 KB |
Output is correct |
5 |
Correct |
4 ms |
1528 KB |
Output is correct |
6 |
Correct |
4 ms |
1528 KB |
Output is correct |
7 |
Correct |
4 ms |
1532 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
11896 KB |
Output is correct |
2 |
Correct |
20 ms |
11896 KB |
Output is correct |
3 |
Correct |
20 ms |
11836 KB |
Output is correct |
4 |
Correct |
20 ms |
11896 KB |
Output is correct |
5 |
Correct |
20 ms |
11896 KB |
Output is correct |
6 |
Correct |
16 ms |
8828 KB |
Output is correct |
7 |
Correct |
18 ms |
10076 KB |
Output is correct |
8 |
Correct |
10 ms |
632 KB |
Output is correct |
9 |
Correct |
11 ms |
3192 KB |
Output is correct |
10 |
Correct |
18 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
34812 KB |
Output is correct |
2 |
Correct |
60 ms |
34844 KB |
Output is correct |
3 |
Correct |
56 ms |
34832 KB |
Output is correct |
4 |
Correct |
59 ms |
34832 KB |
Output is correct |
5 |
Correct |
58 ms |
34836 KB |
Output is correct |
6 |
Correct |
54 ms |
30996 KB |
Output is correct |
7 |
Correct |
52 ms |
29100 KB |
Output is correct |
8 |
Correct |
25 ms |
1044 KB |
Output is correct |
9 |
Correct |
24 ms |
916 KB |
Output is correct |
10 |
Correct |
51 ms |
28692 KB |
Output is correct |
11 |
Correct |
56 ms |
34804 KB |
Output is correct |
12 |
Correct |
27 ms |
3988 KB |
Output is correct |