#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <limits.h>
using namespace std;
int64_t binPow (int64_t x, int64_t y, int64_t MOD) {
x %= MOD;
int64_t res = x;
int64_t ans = 1;
while (y > 0) {
if (y & 1) {
ans *= res, ans %= MOD;
}
res *= res, res %= MOD;
y /= 2;
}
return ans;
}
int64_t inv (int64_t x, int64_t MOD) {
return binPow(x, MOD - 2, MOD);
}
vector<int64_t> powr = {1};
vector<int64_t> ipowr = {1};
struct Hasher {
void resz (int mod, int base, string str) {
int n = str.size();
this->sz = n;
this->MOD = mod, this->BASE = base;
this->pref.assign(n + 1, 0);
for (int i = 1; i <= n; i++) {
pref[i] = (pref[i - 1] + (powr[n - i] * (str[i - 1] - 'A' + 1)) % MOD) % MOD;
pref[i] %= MOD;
}
}
int sz;
int MOD;
int BASE;
vector<int64_t> pref;
int64_t query (int l, int r) {
return (ipowr[sz - r - 1] * (pref[r + 1] - pref[l] + MOD) % MOD) % MOD;
}
};
vector<int> dp, w;
vector<vector<int>> adj;
int myMax = 0;
void rec (int curNode) {
if (dp[curNode] != 0) {
return;
}
dp[curNode] = 0;
for (int i: adj[curNode]) {
rec(i);
dp[curNode] = max(dp[curNode], dp[i]);
}
dp[curNode] ++;
myMax = max(myMax, dp[curNode]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while (powr.size() != 1e6 + 5) {
powr.push_back(powr.back() * 31);
powr.back() %= (int)1e9 + 9;
}
ipowr.resize(powr.size());
ipowr[0] = 1; ipowr[1] = inv(powr[1], 1e9 + 9);
for (int i = 2; i < powr.size(); i++) {
ipowr[i] = (ipowr[i - 1] * ipowr[1]) % ((int)1e9 + 9);
}
int N;
cin >> N;
vector<string> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec[i];
}
map<string,int> m;
map<int64_t,int> myMap;
int64_t ans = 0;
adj.resize(N);
dp.assign(N, 0);
w.resize(N);
for (int i = 0; i < N; i++) {
Hasher h1; h1.resz(1e9 + 9, 31, vec[i]);
for (int l = 1; l <= vec[i].size(); l++) {
if (h1.query(0, l - 1) == h1.query(vec[i].size() - l, vec[i].size() - 1)) {
if (myMap.count(h1.query(0, l - 1))) {
adj[i].push_back(myMap[h1.query(0, l - 1)]);
//cout << i << " " << myMap[h1.query(0, l - 1)] << '\n';
}
}
}
myMap[h1.query(0, vec[i].size() - 1)] = i;
}
for (int i= 0; i < vec.size(); i++) {
w[i] = m[vec[i]];
}
for (int i = 0; i < N; i++) {
rec(i);
}
cout << myMax;
}
Compilation message
savez.cpp: In function 'int main()':
savez.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 2; i < powr.size(); i++) {
| ~~^~~~~~~~~~~~~
savez.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int l = 1; l <= vec[i].size(); l++) {
| ~~^~~~~~~~~~~~~~~~
savez.cpp:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i= 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
savez.cpp:87:13: warning: unused variable 'ans' [-Wunused-variable]
87 | int64_t ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16084 KB |
Output is correct |
2 |
Correct |
23 ms |
16068 KB |
Output is correct |
3 |
Correct |
22 ms |
16060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
16068 KB |
Output is correct |
2 |
Correct |
22 ms |
16068 KB |
Output is correct |
3 |
Correct |
24 ms |
16124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
17092 KB |
Output is correct |
2 |
Correct |
53 ms |
17092 KB |
Output is correct |
3 |
Correct |
79 ms |
18988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
16172 KB |
Output is correct |
2 |
Correct |
58 ms |
17836 KB |
Output is correct |
3 |
Correct |
69 ms |
18164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
19032 KB |
Output is correct |
2 |
Correct |
57 ms |
20116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
21348 KB |
Output is correct |
2 |
Correct |
61 ms |
22396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
22976 KB |
Output is correct |
2 |
Correct |
65 ms |
23932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
25076 KB |
Output is correct |
2 |
Correct |
72 ms |
26144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
38532 KB |
Output is correct |
2 |
Correct |
100 ms |
39668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
53644 KB |
Output is correct |
2 |
Correct |
130 ms |
54912 KB |
Output is correct |
3 |
Runtime error |
94 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |