#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll oo = 1e18;
ll n, dp[(1 << 20)], cnt[20][26];
ll calc(ll msk) {
ll c[26] {};
for (ll i = 0; i < 26; ++i)
c[i] = n;
for (ll i = 0; i < n; ++i)
if ((1 << i) & msk) {
for (ll j = 0; j < 26; ++j)
c[j] = min(c[j], cnt[i][j]);
}
ll len = 0;
for (ll i = 0; i < 26; ++i)
len += c[i];
return len;
}
ll solve(ll msk) {
ll &res = dp[msk];
if (res != -1)
return res;
res = oo;
ll pr = calc(msk);
if (__builtin_popcount(msk) == 1)
return res = pr;
for (ll i = (msk - 1) & msk; i > 0; i = (i - 1) & msk) {
ll cur = solve(i) + solve(msk ^ i) - pr;
res = min(res, cur);
}
return res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef _LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif // _LOCAL
memset(dp, -1, sizeof(dp));
cin >> n;
for (ll i = 0; i < n; ++i) {
string s;
cin >> s;
ll m = ll(s.size());
for (ll j = 0; j < m; ++j)
++cnt[i][s[j] - 'a'];
}
cout << solve((1 << n) - 1) + 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Incorrect |
9 ms |
8576 KB |
Output isn't correct |
4 |
Incorrect |
196 ms |
8672 KB |
Output isn't correct |
5 |
Incorrect |
191 ms |
8576 KB |
Output isn't correct |
6 |
Incorrect |
193 ms |
8576 KB |
Output isn't correct |
7 |
Incorrect |
205 ms |
8820 KB |
Output isn't correct |
8 |
Incorrect |
194 ms |
8704 KB |
Output isn't correct |
9 |
Incorrect |
196 ms |
8816 KB |
Output isn't correct |
10 |
Incorrect |
193 ms |
8824 KB |
Output isn't correct |