Submission #241892

# Submission time Handle Problem Language Result Execution time Memory
241892 2020-06-26T09:09:38 Z NONAME Vještica (COCI16_vjestica) C++14
160 / 160
353 ms 796 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

const int oo = (1 << 30);

int n, dp[(1 << 16)], cnt[16][26];

int calc(int msk) {
    int c[26];

    fill(c, c + 26, oo);

    for (int i = 0; i < n; ++i)
        if ((1 << i) & msk) {
            for (int j = 0; j < 26; ++j)
                c[j] = min(c[j], cnt[i][j]);
        }

    int len = 0;

    for (int i = 0; i < 26; ++i)
        len += c[i];

    return len;
}

int solve(int msk) {
    int &res = dp[msk];
    if (res != -1)
        return res;

    res = oo;

    int pr = calc(msk);

    if (__builtin_popcount(msk) == 1)
        return res = pr;

    for (int i = (msk - 1) & msk; i > 0; i = (i - 1) & msk) {
        int cur = solve(i) + solve(msk ^ i) - pr;
        res = min(res, cur);
    }

    return res;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #ifdef _LOCAL
        freopen("in", "r", stdin);
        freopen("out", "w", stdout);
    #endif // _LOCAL

    memset(dp, -1, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;

        int m = int(s.size());

        for (int j = 0; j < m; ++j)
            ++cnt[i][s[j] - 'a'];
    }

    cout << solve((1 << n) - 1) + 1;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 640 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 5 ms 640 KB Output is correct
4 Correct 341 ms 760 KB Output is correct
5 Correct 344 ms 760 KB Output is correct
6 Correct 346 ms 716 KB Output is correct
7 Correct 348 ms 796 KB Output is correct
8 Correct 353 ms 760 KB Output is correct
9 Correct 348 ms 764 KB Output is correct
10 Correct 344 ms 752 KB Output is correct