# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
948149 | Blagoj | Vještica (COCI16_vjestica) | C++17 | 86 ms | 1520 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |