# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
948149 | Blagoj | Vještica (COCI16_vjestica) | C++17 | 86 ms | 1520 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |