Submission #47687

# Submission time Handle Problem Language Result Execution time Memory
47687 2018-05-06T09:46:45 Z tieunhi Vještica (COCI16_vjestica) C++14
160 / 160
369 ms 3140 KB
#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
1 Correct 3 ms 728 KB Output is correct
2 Correct 3 ms 764 KB Output is correct
3 Correct 3 ms 764 KB Output is correct
4 Correct 341 ms 852 KB Output is correct
5 Correct 337 ms 972 KB Output is correct
6 Correct 339 ms 1332 KB Output is correct
7 Correct 343 ms 1864 KB Output is correct
8 Correct 350 ms 2284 KB Output is correct
9 Correct 357 ms 2600 KB Output is correct
10 Correct 369 ms 3140 KB Output is correct