# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
133483 | tdwn | Vještica (COCI16_vjestica) | C++17 | 351 ms | 1276 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 ll long long
#define pb push_back
#define mp make_pair
using namespace std;
int cnt[30][30], n;
int dp[(1<<16)];
int min_val[30];
int solve(int mask) {
if(dp[mask] != -1) return dp[mask];
if(__builtin_popcount(mask) == 1) {
for(int i=0;i<n;i++) {
if(mask&(1<<i)) {
int sum = 0;
for(int j=0;j<26;j++) {
sum += cnt[i][j];
}
return dp[mask] = sum;
}
}
}
for(int i=0;i<26;i++) min_val[i] = INT_MAX;
int intersection = 0;
for(int i=0;i<n;i++) {
if(mask & (1<<i)) {
for(int j=0;j<26;j++)
min_val[j] = min(min_val[j], cnt[i][j]);
}
}
for(int i=0;i<26;i++) intersection += min_val[i];
dp[mask] = 10000000;
int b = 0;
do {
dp[mask] = min(dp[mask], solve(b) + solve(mask^b) - intersection);
} while (b=(b-mask)&mask);
return dp[mask];
}
int main() {
cin>>n;
string str;
for(int i=0;i<n;i++) {
cin>>str;
for(int j=0;j<str.length();j++) {
cnt[i][int(str[j]-'a')]++;
}
}
memset(dp,-1,sizeof(dp));
cout<<solve((1<<n)-1) + 1;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |