#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod1 = (ll)1e9 + 7;
const ll mod2 = (ll)1e9 + 9;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
void solve() {
ll n;
cin >> n;
vector<string> arr(n);
cin >> arr;
ll LEN = 2000000 + 1;
vector<ll> p1(LEN);
p1[0] = 1;
for (int i = 1; i < LEN; i++) {
p1[i] = (p1[i - 1] * BASE) % mod1;
}
vector<int> lens(n);
vector<vector<int>> hash1(n);
for (int i = 0 ; i < n; i++) {
hash1[i].resize(arr[i].size() + 1);
lens[i] = arr[i].size();
ll h1 = 0;
ll j = 1;
for (auto c : arr[i]) {
h1 = (h1 * BASE + c - 'A' + 1) % mod1;
hash1[i][j] = h1;
j++;
}
arr[i].clear();
}
map<int, int> dp;
int answer = 0;
for (int i = 0; i < n; i++) {
int mx = 0;
int len = 1;
while (len <= lens[i]) {
ll h1 = ((ll)hash1[i].back() - (ll)hash1[i][lens[i] - len] * p1[len]) % mod1;
if (h1 < 0) {
h1 += mod1;
}
if (h1 == hash1[i][len] and dp.count(h1) != 0) {
mx = max(mx, dp[h1]);
}
len++;
}
dp[hash1[i].back()] = mx + 1;
answer = max(answer, mx + 1);
}
cout << answer;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
16000 KB |
Output is correct |
2 |
Correct |
24 ms |
16000 KB |
Output is correct |
3 |
Correct |
24 ms |
16000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
16100 KB |
Output is correct |
2 |
Correct |
23 ms |
16044 KB |
Output is correct |
3 |
Correct |
24 ms |
16384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
20992 KB |
Output is correct |
2 |
Correct |
46 ms |
21044 KB |
Output is correct |
3 |
Correct |
74 ms |
21112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16376 KB |
Output is correct |
2 |
Correct |
53 ms |
20860 KB |
Output is correct |
3 |
Correct |
60 ms |
20984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
21604 KB |
Output is correct |
2 |
Correct |
48 ms |
21692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
22520 KB |
Output is correct |
2 |
Correct |
51 ms |
22648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
24236 KB |
Output is correct |
2 |
Correct |
51 ms |
24244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
26216 KB |
Output is correct |
2 |
Correct |
55 ms |
26396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
37624 KB |
Output is correct |
2 |
Correct |
75 ms |
37624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
52184 KB |
Output is correct |
2 |
Correct |
104 ms |
52044 KB |
Output is correct |
3 |
Runtime error |
121 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |