#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
#define int long long
int dumb(string s)
{
int n = (int)s.size();
vector<vector<int>> lcp(n, vector<int>(n, 0));
vector<vector<bool>> ispal(n, vector<bool>(n, 0));
for (int i = 0; i < n; i++)
{
lcp[i][i] = n - i;
}
for (int i = n - 1; i >= 0; i--)
{
for (int j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
int c = 0;
if (i + 1 < n && j + 1 < n)
{
c += lcp[i + 1][j + 1];
}
lcp[i][j] = lcp[j][i] = 1 + c;
}
}
}
for (int i = 0; i < n; i++)
{
ispal[i][i] = 1;
}
for (int i = 0; i + 1 < n; i++)
{
ispal[i][i + 1] = (s[i] == s[i + 1]);
}
for (int len = 3; len <= n; len++)
{
for (int i = 0; i + len - 1 < n; i++)
{
int j = i + len - 1;
ispal[i][j] = ispal[i + 1][j - 1] & (s[i] == s[j]);
}
}
int sol = 0;
for (int len = 1; len <= n; len++)
{
for (int i = 0; i + len - 1 < n; i++)
{
int j = i + len - 1;
if (ispal[i][j])
{
int matches = 0;
for (int k = 0; k < n; k++)
{
matches += (lcp[k][i] >= len);
}
sol = max(sol, len * matches);
}
}
}
return sol;
}
struct Vertex
{
int fail;
int len;
vector<int> kids;
int cnt;
};
vector<Vertex> verts;
vector<int> mt(26, 0);
int smart(string s)
{
verts.clear();
verts.push_back({ 0, -1, mt, 0 });
verts.push_back({ 0, 0, mt, 0 });
assert((int)s.size() >= 1);
s = "$" + s;
assert((int)s.size() >= 2);
int n = (int)s.size();
int curvert = 0;
for (int i = 1; i < n; i++)
{
while (s[i] != s[i - verts[curvert].len - 1])
{
assert(curvert >= 1);
curvert = verts[curvert].fail;
}
assert(s[i] == s[i - verts[curvert].len - 1]);
{
int x = s[i] - 'a';
assert(0 <= x && x < 26);
if (!verts[curvert].kids[x])
{
int fail = verts[curvert].fail;
assert(fail >= 0);
while (s[i] != s[i - verts[fail].len - 1])
{
assert(fail >= 1);
fail = verts[fail].fail;
}
if (!verts[fail].kids[x])
{
assert(fail == 0);
fail = 1;
}
else
{
fail = verts[fail].kids[x];
}
verts.push_back({ fail, verts[curvert].len + 2, mt, 0 });
verts[curvert].kids[x] = (int)verts.size() - 1;
//cout << (int)verts.size() - 1 << " -> " << fail << "\n";
}
curvert = verts[curvert].kids[x];
}
verts[curvert].cnt++;
}
vector<int> q = { 0, 1 };
for (int i = 0; i < (int)q.size(); i++)
{
int a = q[i];
for (int x = 0; x < 26; x++)
{
if (verts[a].kids[x])
{
q.push_back(verts[a].kids[x]);
}
}
}/*
for (auto& x : q)
{
cout << x << " ";
}
cout << "\n";*/
int sol = 0;
for (int _ = (int)q.size() - 1; _ >= 0; _--)
{
int who = q[_];
verts[verts[who].fail].cnt += verts[who].cnt;
sol = max(sol, verts[who].len * verts[who].cnt);
}
return sol;
exit(0);
}
mt19937 rng(228);
int getrng(int l, int r)
{
assert(l <= r);
int x = rng() % (r - l + 1) + l;
assert(l <= x && x <= r);
return x;
}
string gets(int dim)
{
dim = getrng(1, dim);
string s(dim, '?');
for (auto& ch : s)
{
ch = 'a' + getrng(0, 4);
}
return s;
}
signed main()
{
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
string s;
cin >> s;
//cout << dumb(s) << "\n";
cout << smart(s) << "\n";
exit(0);
if (0)
{
string s = "aaa";
cout << smart(s) << "\n";
cout << dumb(s) << "\n";
exit(0);
}
while (1)
{
string s = gets(10);
if (smart(s) != dumb(s))
{
cout << "bad : " << s << "\n";
cout << "me : " << smart(s) << "\n";
cout << "good : " << dumb(s) << "\n";
exit(0);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3212 KB |
Output is correct |
2 |
Correct |
3 ms |
3212 KB |
Output is correct |
3 |
Correct |
3 ms |
3204 KB |
Output is correct |
4 |
Correct |
3 ms |
3204 KB |
Output is correct |
5 |
Correct |
4 ms |
3332 KB |
Output is correct |
6 |
Correct |
3 ms |
3172 KB |
Output is correct |
7 |
Correct |
3 ms |
3204 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
2320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28928 KB |
Output is correct |
2 |
Correct |
25 ms |
29020 KB |
Output is correct |
3 |
Correct |
27 ms |
28968 KB |
Output is correct |
4 |
Correct |
23 ms |
29052 KB |
Output is correct |
5 |
Correct |
29 ms |
29068 KB |
Output is correct |
6 |
Correct |
21 ms |
21556 KB |
Output is correct |
7 |
Correct |
23 ms |
24832 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
7 ms |
7304 KB |
Output is correct |
10 |
Correct |
23 ms |
24828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
87332 KB |
Output is correct |
2 |
Correct |
89 ms |
87468 KB |
Output is correct |
3 |
Correct |
71 ms |
87552 KB |
Output is correct |
4 |
Correct |
70 ms |
87536 KB |
Output is correct |
5 |
Correct |
85 ms |
87436 KB |
Output is correct |
6 |
Correct |
76 ms |
83220 KB |
Output is correct |
7 |
Correct |
84 ms |
71532 KB |
Output is correct |
8 |
Correct |
5 ms |
1628 KB |
Output is correct |
9 |
Correct |
6 ms |
1628 KB |
Output is correct |
10 |
Correct |
76 ms |
70404 KB |
Output is correct |
11 |
Correct |
80 ms |
87428 KB |
Output is correct |
12 |
Correct |
11 ms |
9328 KB |
Output is correct |