#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define dec(x, y) fixed << setprecision((y)) << (x)
#define xx first
#define yy second
#define srt(v) sort((v).begin(), (v).end())
#define srtr(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define popb pop_back
#define sz(a) (int)(a).size()
#define len(a) (int)(a).length()
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n;
int s[16][26];
bool pos[66000];
ll memo[66000];
ll dp(int mask) {
if(__builtin_popcount(mask)==1) {
int l=log2(mask);
ll duz=0LL;
for(int i=0; i<26; ++i) {
duz+=s[l][i];
}
return duz;
}
if(pos[mask]) return memo[mask];
int p[26];
for(int i=0; i<26; ++i) p[i]=2000000;
for(int i=0; i<n; ++i) {
if((1<<i)&mask) {
for(int j=0; j<26; ++j) {
p[j]=min(p[j], s[i][j]);
}
}
}
ll duz=0LL;
for(int i=0; i<26; ++i) duz+=(ll)p[i];
ll mini=2000000000000000LL;
for(int sub=(mask-1)&mask; sub>0; sub=(sub-1)&mask) {
if(__builtin_popcount(sub)<__builtin_popcount(mask^sub)) continue;
mini=min(mini, dp(sub)+dp(mask^sub));
}
pos[mask]=1;
return memo[mask]=mini-duz;
}
int main() {
ios;
cin >> n;
for(int i=0; i<n; ++i) {
string t; cin >> t;
for(int j=0; j<len(t); ++j) {
++s[i][t[j]-'a'];
}
}
cout << dp((1<<n)-1)+1LL;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
682 ms |
896 KB |
Output is correct |
5 |
Correct |
696 ms |
904 KB |
Output is correct |
6 |
Correct |
760 ms |
972 KB |
Output is correct |
7 |
Correct |
709 ms |
1000 KB |
Output is correct |
8 |
Correct |
694 ms |
992 KB |
Output is correct |
9 |
Correct |
690 ms |
992 KB |
Output is correct |
10 |
Correct |
688 ms |
984 KB |
Output is correct |