# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
299010 | mat_v | Vještica (COCI16_vjestica) | C++14 | 241 ms | 832 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>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define xx first
#define yy second
#define pii pair<int,int>
using namespace std;
int n;
int freq[20][30];
int dp[67000];
bool bio[67000];
int mini[30];
int resi(int mask){
if(bio[mask])return dp[mask];
bio[mask] = 1;
int tr = 0;
ff(i,0,25)mini[i] = 1e9;
int kol = 0;
ff(i,0,n - 1){
if((1<<i)&mask){
kol++;
ff(j,0,26){
mini[j] = min(mini[j], freq[i][j]);
}
}
}
ff(j,0,26){
tr += mini[j];
}
// cout << mask << " " << tr << "\n";
if(kol == 1)return dp[mask] = tr;
int p1 = 1e9;
for(int submask = mask; submask; submask = (submask - 1) & mask){
if(submask == mask)continue;
p1 = min(p1, resi(submask) + resi(mask^submask));
}
return dp[mask] = -tr + p1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n;
ff(i,0,n - 1){
string x;
cin >> x;
int m = x.length();
ff(j,0,m - 1)freq[i][x[j] - 'a']++;
}
cout << resi((1<<n) - 1) + 1;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |