#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;
vector<ll> p1(arr.back().size() + 1), p2(arr.back().size() + 1);
p1[0] = 1;
p2[0] = 1;
for (int i = 1; i < p1.size(); i++) {
p1[i] = (p1[i - 1] * BASE) % mod1;
p2[i] = (p2[i - 1] * BASE) % mod2;
}
vector<vector<ll>> hash1(n), hash2(n);
for (int i = 0 ; i < n; i++) {
hash1[i].pb(0);
hash2[i].pb(0);
ll h1 = 0, h2 = 0;
for (auto c : arr[i]) {
h1 = (h1 * BASE + c - 'A' + 1) % mod1;
h2 = (h2 * BASE + c - 'A' + 1) % mod2;
hash1[i].pb(h1);
hash2[i].pb(h2);
}
}
map<pair<ll, ll>, ll> dp;
ll answer = 0;
for (int i = 0; i < n; i++) {
ll mx = 0;
ll len = 0;
while (len <= arr[i].size()) {
ll h1 = (hash1[i].back() - hash1[i][arr[i].size() - len] * p1[len]) % mod1;
ll h2 = (hash2[i].back() - hash2[i][arr[i].size() - len] * p2[len]) % mod2;
if (h1 < 0) {
h1 += mod1;
}
if (h2 < 0) {
h2 += mod2;
}
if (h1 == hash1[i][len] and h2 == hash2[i][len]) {
mx = max(mx, dp[{h1, h2}]);
}
len++;
}
dp[{hash1[i].back(), hash2[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;
}
Compilation message
savez.cpp: In function 'void solve()':
savez.cpp:43:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < p1.size(); i++) {
~~^~~~~~~~~~~
savez.cpp:65:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (len <= arr[i].size()) {
~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
17532 KB |
Output is correct |
2 |
Incorrect |
113 ms |
17536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2048 KB |
Output is correct |
2 |
Correct |
51 ms |
19832 KB |
Output is correct |
3 |
Incorrect |
50 ms |
20856 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
24900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
25848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
75 ms |
28152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
31580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
49124 KB |
Output is correct |
2 |
Correct |
140 ms |
50428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
165 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |