#include <bits/stdc++.h>
using namespace std;
//T+N
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
template<class T> inline void fix(T &x) {if(x >= mod | x <= -mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl
const int MAX_N = 16, ALPH = 30;
ll dp[1 << MAX_N];
ll cnt[MAX_N][ALPH];
ll eq[1 << MAX_N];
int n;
ll rec(ll mask) {
if(dp[mask] != -1) {
return dp[mask];
}
if(mask == 0) {return 0;}
if((mask & (mask - 1)) == 0) {return 0;}
ll currPref = eq[mask];
dp[mask] = mod * mod;
for(int i = (mask - 1) & mask; i > 0; i = (i - 1) & mask) {
chkmin(dp[mask], rec(i) + rec(mask ^ i) + eq[i] + eq[mask ^ i] - 2 * eq[mask]);
//cout << mask << " " << i << " " << (mask ^ i) << " " << dp[mask] << endl;
}
//cout << mask << " " << dp[mask] << endl;
return dp[mask];
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i = 0; i < n; i ++) {
string in;
cin >> in;
for(auto it : in) {
cnt[i][it - 'a'] ++;
}
}
for(int mask = 0; mask < (1 << n); mask ++) {
eq[mask] = 0;
dp[mask] = -1;
for(int c = 0; c < ALPH; c ++) {
int mn = mod;
for(int i = 0; i < n; i ++) if((1 << i) & mask) {
chkmin(mn, cnt[i][c]);
}
eq[mask] += mn;
}
}
cout << rec((1 << n) - 1) + eq[(1 << n) - 1] + 1 << endl;
return 0;
}
Compilation message
vjestica.cpp: In function 'll rec(ll)':
vjestica.cpp:28:8: warning: unused variable 'currPref' [-Wunused-variable]
ll currPref = eq[mask];
^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
308 ms |
1400 KB |
Output is correct |
5 |
Correct |
278 ms |
1400 KB |
Output is correct |
6 |
Correct |
279 ms |
1748 KB |
Output is correct |
7 |
Correct |
285 ms |
1904 KB |
Output is correct |
8 |
Correct |
291 ms |
1912 KB |
Output is correct |
9 |
Correct |
282 ms |
1904 KB |
Output is correct |
10 |
Correct |
282 ms |
2040 KB |
Output is correct |