#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;
const int N = 5e5 + 10;
int n, f[(1 << 20)][20];
string s[N];
bool gd(int x, int y) {
int l1 = int(s[x].size()), l2 = int(s[y].size()), k = 0;
if (max(l1, l2) - min(l1, l2) > 1)
return 0;
while (k < min(l1, l2) && s[x][k] == s[y][k])
++k;
return (k >= max(l1, l2) - 1);
}
int main() {
fast_io;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
}
if (n > 20)
return void(cout << "0\n"), 0;
for (int i = 0; i < (1 << n); ++i)
for (int j = 0; j < n; ++j)
f[i][j] = 0;
for (int i = 0; i < n; ++i)
f[(1 << i)][i] = 1;
for (int msk = 1; msk < (1 << n); ++msk)
for (int i = 0; i < n; ++i) {
if (!(msk & (1 << i)))
continue;
for (int j = 0; j < n; ++j)
if (!(msk & (1 << j)) && gd(i, j))
f[msk | (1 << j)][j] = max(f[msk | (1 << j)][j], f[msk][i] + 1);
}
int ans = 0;
for (int i = 0; i < (1 << n); ++i)
for (int j = 0; j < n; ++j)
ans = max(ans, f[i][j]);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
21120 KB |
Output is correct |
2 |
Correct |
834 ms |
36572 KB |
Output is correct |
3 |
Correct |
580 ms |
36480 KB |
Output is correct |
4 |
Incorrect |
53 ms |
16000 KB |
Output isn't correct |
5 |
Incorrect |
23 ms |
18944 KB |
Output isn't correct |
6 |
Incorrect |
16 ms |
16784 KB |
Output isn't correct |
7 |
Incorrect |
17 ms |
16548 KB |
Output isn't correct |
8 |
Incorrect |
16 ms |
16496 KB |
Output isn't correct |
9 |
Incorrect |
24 ms |
19292 KB |
Output isn't correct |
10 |
Incorrect |
17 ms |
16708 KB |
Output isn't correct |