#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 6e5 + 20;
string S;
int N, gap;
int sa[maxn], pos[maxn], tmp[maxn], lcp[maxn] , mn[maxn * 4];
vector<int> adj[maxn];
int sz[maxn] , w[maxn] , id = 1;
inline bool sufCmp(int i, int j)
{
if (pos[i] != pos[j])
return pos[i] < pos[j];
i += gap;
j += gap;
return (i < N && j < N) ? pos[i] < pos[j] : i > j;
}
inline void buildSA()
{
N = S.size();
for(int i = 0; i < N; i++) sa[i] = i, pos[i] = S[i];
for (gap = 1;; gap *= 2)
{
sort(sa, sa + N, sufCmp);
for(int i = 0; i < N - 1; i++) tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
for(int i = 0; i < N; i++) pos[sa[i]] = tmp[i];
if (tmp[N - 1] == N - 1) break;
}
}
inline void buildLCP()
{
for (int i = 0, k = 0; i < N; ++i) if (pos[i] != N - 1)
{
for (int j = sa[pos[i] + 1]; S[i + k] == S[j + k];)
++k;
lcp[pos[i]] = k;
if (k)--k;
}
}
void buildSeg(int s = 0 , int e = N - 1 , int v = 1)
{
if(e - s < 2)
{
mn[v] = lcp[s];
return;
}
int m = (s + e) / 2;
buildSeg(s , m , 2 * v);
buildSeg(m , e , 2 * v + 1);
mn[v] = min(mn[2 * v] , mn[2 * v + 1]);
}
int GetLcp(int l , int r , int s = 0 , int e = N - 1 , int v = 1)
{
if(l <= s && e <= r)
return mn[v];
if(r <= s || e <= l)
return maxn;
int m = (s + e) / 2;
return min(GetLcp(l , r , s , m , 2 * v) , GetLcp(l , r , m , e , 2 * v + 1));
}
void buildTrie(int l = 0 , int r = N - 1 , int root = 0 , int K = 0)
{
if(l == r)
{
if(N - sa[l] != K)
{
adj[root].pb(id);
w[id] = N - sa[l] - K;
sz[id++] = 1;
}
else
sz[root]++;
return;
}
int lcp = GetLcp(l , r) - K;
if(lcp != 0)
{
adj[root].pb(id);
w[id] = lcp;
id++;
buildTrie(l , r , id - 1 , K + lcp);
return;
}
if(GetLcp(l , l + 1) - K == 0)
{
buildTrie(l , l , root , K);
buildTrie(l + 1 , r , root , K);
return;
}
int L = l + 1 , R = r;
for(int i = r - 1; i >= l; i--)
if(GetLcp(l , i) - K != 0)
{
buildTrie(l , i , root , K);
buildTrie(i + 1 , r , root , K);
return;
}
while(R - L > 1)
{
int M = (L + R) / 2;
if(GetLcp(l , M) - K == 0)
R = M;
else
L = M;
}
buildTrie(l , L , root , K);
buildTrie(L + 1 , r , root , K);
}
ll res;
void dfs(int v , int d = 0)
{
for(auto u : adj[v])
{
dfs(u , d + w[u]);
sz[v] += sz[u];
}
res = max(res , 1LL * d * sz[v]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> S;
N = S.size();
/*
buildSA();
buildLCP();
buildSeg();
buildTrie();
dfs(0);*/
for(int l = 0; l < N; l++)
for(int r = l; r < N; r++)
{
string t = S.substr(l , r - l + 1);
reverse(t.begin() , t.end());
if(t != S.substr(l , r - l + 1))
continue;
int x = 0;
for(int i = 0; i + r - l + 1 <= N; i++)
if(t == S.substr(i , r - l + 1))
x++;
res = max(res , 1LL * x * (r - l + 1));
}
cout << res << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14580 KB |
Output is correct |
3 |
Correct |
18 ms |
14580 KB |
Output is correct |
4 |
Correct |
14 ms |
14592 KB |
Output is correct |
5 |
Correct |
14 ms |
14688 KB |
Output is correct |
6 |
Correct |
14 ms |
14700 KB |
Output is correct |
7 |
Correct |
14 ms |
14700 KB |
Output is correct |
8 |
Correct |
14 ms |
14860 KB |
Output is correct |
9 |
Correct |
14 ms |
14868 KB |
Output is correct |
10 |
Correct |
14 ms |
14868 KB |
Output is correct |
11 |
Correct |
14 ms |
14868 KB |
Output is correct |
12 |
Correct |
14 ms |
14868 KB |
Output is correct |
13 |
Correct |
14 ms |
14868 KB |
Output is correct |
14 |
Correct |
14 ms |
14868 KB |
Output is correct |
15 |
Correct |
14 ms |
14868 KB |
Output is correct |
16 |
Correct |
14 ms |
14868 KB |
Output is correct |
17 |
Correct |
14 ms |
14868 KB |
Output is correct |
18 |
Correct |
15 ms |
14868 KB |
Output is correct |
19 |
Correct |
21 ms |
14868 KB |
Output is correct |
20 |
Correct |
21 ms |
14872 KB |
Output is correct |
21 |
Correct |
16 ms |
14876 KB |
Output is correct |
22 |
Correct |
15 ms |
14880 KB |
Output is correct |
23 |
Correct |
29 ms |
14884 KB |
Output is correct |
24 |
Correct |
18 ms |
14888 KB |
Output is correct |
25 |
Correct |
26 ms |
14892 KB |
Output is correct |
26 |
Correct |
15 ms |
14896 KB |
Output is correct |
27 |
Correct |
15 ms |
14900 KB |
Output is correct |
28 |
Correct |
19 ms |
14936 KB |
Output is correct |
29 |
Correct |
17 ms |
14936 KB |
Output is correct |
30 |
Correct |
15 ms |
14936 KB |
Output is correct |
31 |
Correct |
14 ms |
14936 KB |
Output is correct |
32 |
Correct |
15 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1071 ms |
14936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
14936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1064 ms |
15052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1085 ms |
15572 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |