/**
* author: wxhtzdy
* created: 17.07.2023 21:06:38
**/
#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];
}
vector<vector<bool>> f(n, vector<bool>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int si = (int) s[i].size();
int sj = (int) s[j].size();
int ptr = 0;
while (ptr < min(si, sj) && s[i][si - ptr - 1] == s[j][sj - ptr - 1]) {
ptr += 1;
}
f[i][j] = f[j][i] = (ptr >= max(si, sj) - 1);
}
}
vector<vector<bool>> dp(1 << n, vector<bool>(n));
for (int i = 0; i < n; i++) {
dp[1 << i][i] = true;
}
for (int mask = 1; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
if (!dp[mask][i]) {
continue;
}
for (int j = 0; j < n; j++) {
if (mask >> j & 1) {
continue;
}
if (f[i][j]) {
dp[mask ^ (1 << j)][j] = true;
}
}
}
}
int ans = 0;
for (int mask = 0; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
if (dp[mask][i]) {
ans = max(ans, __builtin_popcount(mask));
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4948 KB |
Output is correct |
2 |
Correct |
27 ms |
18772 KB |
Output is correct |
3 |
Correct |
26 ms |
18724 KB |
Output is correct |
4 |
Runtime error |
132 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Execution timed out |
1059 ms |
6996 KB |
Time limit exceeded |
6 |
Runtime error |
275 ms |
13924 KB |
Execution killed with signal 11 |
7 |
Runtime error |
157 ms |
3020 KB |
Execution killed with signal 6 |
8 |
Runtime error |
64 ms |
2208 KB |
Execution killed with signal 11 |
9 |
Execution timed out |
1067 ms |
34628 KB |
Time limit exceeded |
10 |
Runtime error |
88 ms |
2212 KB |
Execution killed with signal 11 |