#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int len[n], cnt[n][26];
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) {
string s;
cin >> s;
len[i] = s.size();
for (auto x : s) cnt[i][x - 'a']++;
}
ll dp[1 << n];
for (int mask = 1; mask < (1 << n); mask++) {
if (__builtin_popcount(mask) == 1) {
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) dp[mask] = len[j];
}
continue;
}
int sum = 0;
int mn[26];
for (int j = 0; j < 26; j++) mn[j] = 1e9;
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) {
for (int k = 0; k < 26; k++) mn[k] = min(mn[k], cnt[j][k]);
}
}
for (int j = 0; j < 26; j++) sum += mn[j];
dp[mask] = 1e9;
for (int sub = mask & (mask - 1); sub; sub = mask & (sub - 1)) {
dp[mask] = min(dp[mask], dp[sub] + dp[mask ^ sub] - sum);
}
}
cout << dp[(1 << n) - 1] + 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
80 ms |
968 KB |
Output is correct |
5 |
Correct |
81 ms |
1056 KB |
Output is correct |
6 |
Correct |
79 ms |
1116 KB |
Output is correct |
7 |
Correct |
78 ms |
1368 KB |
Output is correct |
8 |
Correct |
81 ms |
1496 KB |
Output is correct |
9 |
Correct |
86 ms |
1520 KB |
Output is correct |
10 |
Correct |
78 ms |
1456 KB |
Output is correct |