Submission #154700

# Submission time Handle Problem Language Result Execution time Memory
154700 2019-09-23T15:19:27 Z farmerboy Vještica (COCI16_vjestica) C++14
160 / 160
244 ms 1912 KB
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 20;
const int MAXM = 1000010;
const int MAXK = 110;
const int MAXQ = 200010;

int n, cnt[MAXN][26], tmpCnt[26], common[70000], dp[70000];
string s[MAXN];

int solve(int mask) {
    if (dp[mask] != -1) return dp[mask];
    int cm = common[mask];
    int res = 1000000000;
    for (int split = mask & (mask-1); split; split = (split-1) & mask) {
        int submask2 = mask ^ split;
        res = min(res, solve(split) + solve(submask2) - cm);
    }
    return dp[mask] = res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n;
    FOR(i,1,n) cin >> s[i];

    FOR(i,1,n)
        FOR(j,0,SZ(s[i])-1) cnt[i][s[i][j] - 'a']++;

    FOR(mask,1,(1<<n)-1) {
        FOR(i,0,25) tmpCnt[i] = 1000000000;
        FOR(i,1,n)
            if (mask & (1<<(i-1)))
                FOR(j,0,25) tmpCnt[j] = min(tmpCnt[j], cnt[i][j]);
        FOR(i,0,25) common[mask] += tmpCnt[i];
    }

    FOR(i,1,(1<<n)-1) {
        if (__builtin_popcount(i) == 1) {
            FOR(j,1,n)
                if (i & (1<<(j-1))) dp[i] = SZ(s[j]);
        }
        else dp[i] = -1;
    }

    cout << solve((1<<n)-1) + 1;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 237 ms 888 KB Output is correct
5 Correct 239 ms 1068 KB Output is correct
6 Correct 240 ms 1400 KB Output is correct
7 Correct 242 ms 1912 KB Output is correct
8 Correct 241 ms 1884 KB Output is correct
9 Correct 244 ms 1912 KB Output is correct
10 Correct 243 ms 1784 KB Output is correct