#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n;
string s[20];
ll cnt[20][30]={0};
ll dp[1<<16];
void solve()
{
cin >> n;
for(int i=0;i<n;i++) cin >> s[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<s[i].size();j++)
{
cnt[i][s[i][j]-'a']++;
}
}
dp[0]=0;
for(int i=1;i<(1<<n);i++)
{
dp[i]=inf;
ll mn[26];
for(int j=0;j<26;j++) mn[j]=1e8;
for(int j=0;j<n;j++)
{
if(i>>j&1)
{
for(int k=0;k<26;k++)
{
mn[k]=min(mn[k],cnt[j][k]);
}
}
}
ll sum=0;
for(int j=0;j<26;j++)
{
sum+=mn[j];
}
//bool ok=true;
if(__builtin_popcount(i)==1)
{
dp[i]=sum;
continue;
}
for(int mask=i&-i;i-mask;mask=i&(mask-i))
{
dp[i]=min(dp[i],dp[mask]+dp[i-mask]-sum);
}
//if(ok) dp[i]=sum;
//if(i==2) cout <<<<'\n';
}
cout << dp[(1<<n)-1]+1;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
vjestica.cpp: In function 'void solve()':
vjestica.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int j=0;j<s[i].size();j++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
85 ms |
716 KB |
Output is correct |
5 |
Correct |
85 ms |
848 KB |
Output is correct |
6 |
Correct |
91 ms |
1036 KB |
Output is correct |
7 |
Correct |
86 ms |
1208 KB |
Output is correct |
8 |
Correct |
86 ms |
1336 KB |
Output is correct |
9 |
Correct |
91 ms |
1320 KB |
Output is correct |
10 |
Correct |
93 ms |
1336 KB |
Output is correct |