#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n;
string s[maxn];
int dp[maxn];
int idx = 0;
int kraj[20*maxn];
int lipa[20*maxn][26];
void add(string a, int id){
int tr = 0;
ff(i,0,sz(a) - 1){
if(!lipa[tr][a[i] - 'a'])lipa[tr][a[i] - 'a'] = ++idx;
tr = lipa[tr][a[i] - 'a'];
}
kraj[tr] = max(kraj[tr], dp[id]);
}
int kve(string a){
int tr = 0;
int pomtr = -1;
int ide = 0;
ff(i,0,sz(a) - 1){
if(i == sz(a) - 1)pomtr = tr;
if(!lipa[tr][a[i] - 'a'])break;
ide = i;
tr = lipa[tr][a[i] - 'a'];
}
// gledam za jednu ispred
int ans = 0;
if(pomtr != -1){
ff(i,0,25){
int ntr = lipa[pomtr][i];
if(ntr){
ans = max(ans, kraj[ntr]);
}
}
}
// gledam za jednu ispod
if(ide == sz(a) - 1){
ff(i,0,25){
int ntr = lipa[tr][i];
if(ntr)ans = max(ans, kraj[ntr]);
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n;
ff(i,1,n)cin >> s[i];
ff(i,1,n){
string x = s[i];
reverse(all(x));
dp[i] = kve(x) + 1;
add(x, i);
}
int rez = 0;
ff(i,1,n)rez = max(rez, dp[i]);
cout << rez << endl;
return 0;
}
/**
4
honi
toni
oni
ovi
// probati bojenje sahovski ili slicno
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
3564 KB |
Output isn't correct |
2 |
Correct |
3 ms |
3564 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3564 KB |
Output isn't correct |
4 |
Runtime error |
26 ms |
7532 KB |
Execution killed with signal 11 |
5 |
Incorrect |
42 ms |
9580 KB |
Output isn't correct |
6 |
Incorrect |
38 ms |
36060 KB |
Output isn't correct |
7 |
Incorrect |
36 ms |
35816 KB |
Output isn't correct |
8 |
Incorrect |
32 ms |
35616 KB |
Output isn't correct |
9 |
Incorrect |
83 ms |
42088 KB |
Output isn't correct |
10 |
Incorrect |
33 ms |
35656 KB |
Output isn't correct |