#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 20, MASK = (1 << 16) + 5;
string s[N];
int n, cnt[N][26], ans[MASK], lcp[MASK];
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
fw (i, 0, n) {
cin >> s[i];
fa (j, s[i]) cnt[i][j - 'a']++;
}
fw (i, 1, (1 << n)) {
fw (j, 0, 26) {
int mn = inf;
fw (k, 0, n) if (i & (1 << k)) mn = min(mn, cnt[k][j]);
lcp[i] += mn;
}
}
ans[0] = inf;
fw (i, 1, (1 << n)) {
int totalLen = 0;
fw (j, 0, n) if (i & (1 << j)) totalLen += s[j].length();
totalLen -= (bp(i) - 1) * lcp[i];
ans[i] = totalLen;
for (int submask = i; submask; submask = (submask - 1) & i) {
ans[i] = min(ans[i], ans[submask] + ans[i ^ submask] - lcp[i]);
}
// cout << "ans[" << i << "] = " << ans[i] << "\n";
}
cout << ans[(1 << n) - 1] + 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
126 ms |
888 KB |
Output is correct |
5 |
Correct |
126 ms |
1044 KB |
Output is correct |
6 |
Correct |
128 ms |
1420 KB |
Output is correct |
7 |
Correct |
128 ms |
1756 KB |
Output is correct |
8 |
Correct |
128 ms |
1784 KB |
Output is correct |
9 |
Correct |
129 ms |
1824 KB |
Output is correct |
10 |
Correct |
128 ms |
1912 KB |
Output is correct |