#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3SgiE60 ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
const int inf=1e9;
signed main(){
_3SgiE60;
int n;
cin>>n;
vec(vi) a(n,vi(26));
vi dp((1<<n),inf);
rep(i,n){
string s;
cin>>s;
dp[(1<<i)]=sz(s);
rep(j,sz(s)){
a[i][s[j]-'a']+=1;
}
}
rep(msk,(1<<n)){
vi rbe(26,1e9);
int sun=0;
rep(ch,26){
rep(i,n){
if(msk>>i&1){
rbe[ch]=min(rbe[ch],a[i][ch]);
}
}
sun+=rbe[ch];
}
for(int nmsk=msk;nmsk;nmsk=(nmsk-1)&msk){
int now=dp[nmsk]+dp[msk^nmsk];
// if(msk==3) print(msk,nmsk);
now-=sun;
dp[msk]=min(dp[msk],now);
}
}
// print(dp[3]);
print(dp[(1<<n)-1]+1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
104 ms |
596 KB |
Output is correct |
5 |
Correct |
101 ms |
596 KB |
Output is correct |
6 |
Correct |
112 ms |
888 KB |
Output is correct |
7 |
Correct |
106 ms |
980 KB |
Output is correct |
8 |
Correct |
105 ms |
1108 KB |
Output is correct |
9 |
Correct |
107 ms |
1104 KB |
Output is correct |
10 |
Correct |
103 ms |
1108 KB |
Output is correct |