#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
/*
if a < b and b < c, then a < c
use string hashing
*/
const int m = 27;
const ll mod = 1e7 + 7;
ll powm(ll y) {
ll x = m;
ll res = 1;
while (y) {
if (y & 1) res = (res * x) % mod;
y >>= 1;
x = (x * x) % mod;
}
return res;
}
int n;
vt<int> dp;
void push_back(pl& hash, int c) {
hash = {(hash.f * m + c + 1) % mod, hash.s + 1};
}
void push_front(pl& hash, int c) {
hash = {(powm(hash.s) * (c + 1) + hash.f) % mod, hash.s + 1};
}
const pl hid = {0, 0};
int arr[mod];
main() {
cin.tie(0)->sync_with_stdio(0);
memset(arr, -1, sizeof(arr));
cin >> n;
dp.resize(n, 1);
F0R (i, n) {
pl lhs = hid, rhs = hid;
string st; cin >> st;
F0R (j, size(st)) {
push_back(lhs, st[j] - 'A');
push_front(rhs, st[size(st) - j - 1] - 'A');
if (lhs.f == rhs.f) {
if (arr[lhs.f] != -1) {
dp[i] = max(dp[arr[lhs.f]] + 1, dp[i]);
}
}
}
arr[lhs.f] = i;
}
cout << *max_element(all(dp)) << endl;
}
Compilation message
savez.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
39516 KB |
Output is correct |
2 |
Correct |
18 ms |
39516 KB |
Output is correct |
3 |
Correct |
18 ms |
39504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
39392 KB |
Output is correct |
2 |
Correct |
17 ms |
39512 KB |
Output is correct |
3 |
Correct |
21 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
39592 KB |
Output is correct |
2 |
Correct |
56 ms |
39516 KB |
Output is correct |
3 |
Correct |
56 ms |
39512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
39512 KB |
Output is correct |
2 |
Correct |
64 ms |
39608 KB |
Output is correct |
3 |
Correct |
60 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
39512 KB |
Output is correct |
2 |
Correct |
39 ms |
39512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
39512 KB |
Output is correct |
2 |
Correct |
34 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
39772 KB |
Output is correct |
2 |
Correct |
34 ms |
39512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
39768 KB |
Output is correct |
2 |
Correct |
33 ms |
39772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
40536 KB |
Output is correct |
2 |
Correct |
36 ms |
40540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
41052 KB |
Output is correct |
2 |
Correct |
42 ms |
40960 KB |
Output is correct |
3 |
Correct |
60 ms |
43352 KB |
Output is correct |