# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
47687 | tieunhi | Vještica (COCI16_vjestica) | C++14 | 369 ms | 3140 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 N 16
using namespace std;
int n, a[N][26], sz[N];
int dp[(1 << N)];
int solve(int mask)
{
if (dp[mask] != -1)
return dp[mask];
if (mask == 0)
return (dp[mask] = 0);
if (__builtin_popcount(mask) == 1)
{
for (int i = 0; i < n; i++)
if ((mask >> i) & 1)
return (dp[mask] = sz[i]);
}
int common = 0;
for (int j = 0; j < 26; j++)
{
int x = 1e9;
for (int i = 0; i < n; i++)
if ((mask >> i) & 1)
x = min(x, a[i][j]);
common += x;
}
dp[mask] = 1e9;
for (int i = (mask-1)&mask; i; i = (i-1)&mask)
dp[mask] = min(dp[mask], solve(i) + solve(mask ^ i) - common);
return dp[mask];
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
cin >> n;
for (int i = 0; i < n; i++)
{
string s; cin >> s;
sz[i] = s.size();
for (int j = 0; j < sz[i]; j++)
a[i][s[j] - 'a']++;
}
memset(dp, -1, sizeof dp);
cout <<solve((1 << n) - 1) + 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |