# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
167674 | shuvi_dola | Vještica (COCI16_vjestica) | C++14 | 155 ms | 1868 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;
const int N = (1 << 16) + 3;
const int inf = 1e9 + 8;
int dp[N], same[N];
string s[20];
int cnt[20][30];
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s[i];
for(int j = 0; j < (int)s[i].length(); j++) {
cnt[i][s[i][j] - 'a']++;
}
}
for(int mask = 0; mask < (1 << n); mask++) {
for(int i = 0; i < 26; i++) {
int mnsame = inf;
for(int j = 0; j < n; j++) {
if((mask & (1 << j))) {
mnsame = min(mnsame, cnt[j][i]);
}
}
same[mask] += mnsame;
}
}
dp[0] = inf;
for(int mask = 0; mask < (1 << n); mask++) {
if(__builtin_popcount(mask) == 1) {
for(int i = 0; i < n; i++) {
if((mask & (1 << i))) {
dp[mask] = s[i].length();
}
}
}
else {
dp[mask] = inf;
for(int submask = mask; submask > 0; submask = (submask - 1) & mask) {
dp[mask] = min(dp[mask], dp[submask] + dp[submask ^ mask] - same[mask]);
}
}
}
cout << dp[(1 << n) - 1] + 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |