#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
reverse(s[i].begin(), s[i].end());
}
auto Good = [&](string s, string t) {
if (s.size() > t.size()) {
swap(s, t);
}
int p = 0;
while (p < (int) s.size() && s[p] == t[p]) {
p += 1;
}
return p >= (int) t.size() - 1;
};
sort(s.begin(), s.end(), [&](string a, string b) {
if ((int) a.size() != (int) b.size()) {
return (int) a.size() < (int) b.size();
} else {
return a < b;
}
});
vector<int> dp(n);
for (int i = 0; i < n; i++) {
int mx = 0;
for (int j = 0; j < i; j++) {
if (Good(s[i], s[j])) {
mx = max(mx, dp[j]);
}
}
dp[i] = mx + 1;
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Execution timed out |
1099 ms |
17904 KB |
Time limit exceeded |
5 |
Execution timed out |
1040 ms |
3584 KB |
Time limit exceeded |
6 |
Incorrect |
222 ms |
1268 KB |
Output isn't correct |
7 |
Incorrect |
172 ms |
1324 KB |
Output isn't correct |
8 |
Incorrect |
69 ms |
1224 KB |
Output isn't correct |
9 |
Execution timed out |
1066 ms |
4280 KB |
Time limit exceeded |
10 |
Incorrect |
74 ms |
1120 KB |
Output isn't correct |