#include <bits/stdc++.h>
#define MAXN 500010
#define pb push_back
#define inf 0x3f3f3f3f
using namespace std;
int n, dp[MAXN], sizes[3000010];
vector < string > vet;
inline bool cmp(string a, string b)
{
if(a.size() > b.size()) return true;
else if(a.size() < b.size()) return false;
return a < b;
}
inline int func(int i)
{
//cout << i << endl;
if(i == n-1) return 1;
if(dp[i] != -1) return dp[i];
string eu = vet[i];
eu.pop_back();
int r = 1;
if(vet[i+1].size() - 1 == eu.size())
{
string k = vet[i+1];
k.pop_back();
if(k == eu) r = func(i+1) + 1;
//cout << k << endl;
}
int ind = sizes[eu.size()];
//cout << ind << endl;
if(ind != -1 and vet[ind].size() == eu.size())
{
if(vet[ind] == eu) r = max(r, func(ind) + 1);
}
return dp[i] = r;
}
int main()
{
cin >> n;
memset(dp, -1, sizeof dp);
memset(sizes, -1, sizeof sizes);
for(int i = 0 ; i < n ; i++)
{
string s;
cin >> s;
reverse(s.begin(), s.end());
vet.pb(s);
}
sort(vet.begin(), vet.end(), cmp);
for(int i = 0 ; i < n ; i++)
{
//cout << vet[i] << endl;
if(sizes[vet[i].size()] == -1) sizes[vet[i].size()] = i;
}
//cout << sizes[3] << ' ' << sizes[4] << endl;
int resp = 0;
for(int i = 0 ; i < n ; i++)
{
resp = max(resp, func(i));
// cout << i << ' ' << resp << endl;
}
cout << resp << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
13952 KB |
Output is correct |
2 |
Correct |
13 ms |
14080 KB |
Output is correct |
3 |
Incorrect |
13 ms |
14208 KB |
Output isn't correct |
4 |
Incorrect |
710 ms |
30772 KB |
Output isn't correct |
5 |
Correct |
103 ms |
17892 KB |
Output is correct |
6 |
Incorrect |
36 ms |
15212 KB |
Output isn't correct |
7 |
Incorrect |
33 ms |
15148 KB |
Output isn't correct |
8 |
Incorrect |
28 ms |
15032 KB |
Output isn't correct |
9 |
Incorrect |
152 ms |
18328 KB |
Output isn't correct |
10 |
Incorrect |
33 ms |
15132 KB |
Output isn't correct |