Submission #167674

#TimeUsernameProblemLanguageResultExecution timeMemory
167674shuvi_dolaVještica (COCI16_vjestica)C++14
160 / 160
155 ms1868 KiB
#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 timeMemoryGrader output
Fetching results...