#include <bits/stdc++.h>
#define task "R"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5e5 + 2, mod = 1e9 + 7, NN = 3e6 + 2;
int n, dp[N], pw[NN], Base = 31;
set<ii> hs[6000][2];
string s[N];
vi leng;
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n;
rep(i, 1, n)
{
cin >> s[i];
leng.eb(SZ(s[i]));
leng.eb(SZ(s[i]) - 1);
leng.eb(SZ(s[i]) + 1);
reverse(all(s[i]));
}
sort(all(leng));
leng.erase(unique(all(leng)), leng.end());
pw[0] = 1;
rep(i, 1, NN - 2) pw[i] = pw[i - 1] * 1LL * Base % mod;
rep(i, 1, n)
{
int h = 0, len = SZ(s[i]);
rep(j, 0, len - 2) h = (h + pw[j] * 1LL * s[i][j]) % mod;
int h1 = (h + pw[len - 1] * 1LL * s[i].back()) % mod;
int idlen = lower_bound(all(leng), len) - leng.begin(), near1 = 0, near2 = 0;
auto it = hs[idlen - 1][0].lower_bound(ii(h, -1));
if (it != hs[idlen - 1][0].end() && it->F == h) near1 = it->S;
it = hs[idlen][1].lower_bound(ii(h, -1));
if (it != hs[idlen][1].end() && it->F == h) near1 = max(near1, it->S), hs[idlen][1].erase(it);
it = hs[idlen + 1][1].lower_bound(ii(h1, -1));
if (it != hs[idlen + 1][1].end() && it->F == h1) near2 = it->S;
hs[idlen][0].insert(ii(h1, i));
hs[idlen][1].insert(ii(h, i));
dp[i] = max(dp[near1], dp[near2]) + 1;
}
cout << *max_element(dp + 1, dp + 1 + n);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
28280 KB |
Output isn't correct |
2 |
Correct |
34 ms |
28408 KB |
Output is correct |
3 |
Incorrect |
34 ms |
28280 KB |
Output isn't correct |
4 |
Execution timed out |
1098 ms |
64180 KB |
Time limit exceeded |
5 |
Incorrect |
60 ms |
34424 KB |
Output isn't correct |
6 |
Incorrect |
41 ms |
29772 KB |
Output isn't correct |
7 |
Incorrect |
39 ms |
29464 KB |
Output isn't correct |
8 |
Incorrect |
38 ms |
29268 KB |
Output isn't correct |
9 |
Incorrect |
72 ms |
35536 KB |
Output isn't correct |
10 |
Incorrect |
38 ms |
29308 KB |
Output isn't correct |