제출 #167649

#제출 시각아이디문제언어결과실행 시간메모리
167649DrumpfTheGodEmperorVještica (COCI16_vjestica)C++14
160 / 160
129 ms1912 KiB
#include <bits/stdc++.h> #define bp __builtin_popcountll #define pb push_back #define in(s) freopen(s, "r", stdin); #define out(s) freopen(s, "w", stdout); #define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\ freopen((string(s) + "." + end2).c_str(), "w", stdout); #define fi first #define se second #define bw(i, r, l) for (int i = r - 1; i >= l; i--) #define fw(i, l, r) for (int i = l; i < r; i++) #define fa(i, x) for (auto i: x) using namespace std; const int mod = 1e9 + 7, inf = 1061109567; const long long infll = 4557430888798830399; const int N = 20, MASK = (1 << 16) + 5; string s[N]; int n, cnt[N][26], ans[MASK], lcp[MASK]; signed main() { #ifdef BLU in("blu.inp"); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; fw (i, 0, n) { cin >> s[i]; fa (j, s[i]) cnt[i][j - 'a']++; } fw (i, 1, (1 << n)) { fw (j, 0, 26) { int mn = inf; fw (k, 0, n) if (i & (1 << k)) mn = min(mn, cnt[k][j]); lcp[i] += mn; } } ans[0] = inf; fw (i, 1, (1 << n)) { int totalLen = 0; fw (j, 0, n) if (i & (1 << j)) totalLen += s[j].length(); totalLen -= (bp(i) - 1) * lcp[i]; ans[i] = totalLen; for (int submask = i; submask; submask = (submask - 1) & i) { ans[i] = min(ans[i], ans[submask] + ans[i ^ submask] - lcp[i]); } // cout << "ans[" << i << "] = " << ans[i] << "\n"; } cout << ans[(1 << n) - 1] + 1; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...