#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
int N;
int C[16][26];
int dp[1<<16];
int L[1<<16][26];
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N;
rep(i, N) {
string s;
cin >> s;
for (char c : s) C[i][c-'a']++;
}
dp[0] = 0;
for (int S=1; S<(1<<N); S++) {
int popcount = __builtin_popcount(S);
rep(j, 26) {
int m = INF;
rep(i, N) if ((S>>i)&1) m = min(m, C[i][j]);
L[S][j] = m;
}
int c = 0;
rep(j, 26) c += L[S][j];
if (popcount == 1) {
dp[S] = c;
continue;
}
int m = INF;
for (int T=(S-1)&S; T>0; T=(T-1)&S) m = min(m, dp[T] + dp[S^T]);
dp[S] = m - c;
}
cout << dp[(1<<N)-1]+1 << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9088 KB |
Output is correct |
2 |
Correct |
0 ms |
9088 KB |
Output is correct |
3 |
Correct |
0 ms |
9088 KB |
Output is correct |
4 |
Correct |
133 ms |
9088 KB |
Output is correct |
5 |
Correct |
129 ms |
9088 KB |
Output is correct |
6 |
Correct |
153 ms |
9088 KB |
Output is correct |
7 |
Correct |
133 ms |
9288 KB |
Output is correct |
8 |
Correct |
136 ms |
9236 KB |
Output is correct |
9 |
Correct |
136 ms |
9260 KB |
Output is correct |
10 |
Correct |
126 ms |
9240 KB |
Output is correct |