# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
168682 | dolphingarlic | Vještica (COCI16_vjestica) | C++14 | 2073 ms | 508 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
COCI 2016 Vjestica
- Firstly, notice that N is very small. This implies that we should use a bitmask
- For each mask, we split the active words into 2 groups and solve recursively for those
- solve(mask) = min(solve(A) + solve(B)) where A + B = mask, A & mask == A, B & mask == B, and A & B == 0
- Complexity: O(N log N * 2^N)
*/
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n;
unordered_map<char, int> cnt[16];
int solve(int mask) {
int ans = INT_MAX, common = 0;
if (!mask) return 0;
unordered_map<char, int> diff;
for (char i = 'a'; i <= 'z'; i++) {
diff[i] = INT_MAX;
FOR(j, 0, n) if (mask & (1 << j)) diff[i] = min(diff[i], cnt[j][i]);
common += diff[i];
}
FOR(i, 0, n) {
if (mask & (1 << i)) {
if (diff == cnt[i]) mask -= (1<<i);
else for (char j = 'a'; j <= 'z'; j++) cnt[i][j] -= diff[j];
}
}
if (!mask) return common;
FOR(i, (common == 0), mask) {
if ((mask & i) == i) ans = min(ans, solve(i) + solve(mask - i));
}
FOR(i, 0, n) {
if (mask & (1 << i)) {
for (char j = 'a'; j <= 'z'; j++) cnt[i][j] += diff[j];
}
}
return ans + common;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
FOR(i, 0, n) {
for (char j = 'a'; j <= 'z'; j++) cnt[i][j] = 0;
string s;
cin >> s;
for (char j : s) cnt[i][j]++;
}
cout << solve((1 << n) - 1) + 1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |