#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']++;
}
}
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;
for(int k=0;k<26;k++)
{
ll mask=0;
for(int j=0;j<n;j++)
{
if(i>>j&1)
{
if(cnt[j][k]==mn[k])
{
mask+=(1<<j);
}
}
}
if(mask!=i&&mask!=0)
{
ok=false;
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++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
77 ms |
740 KB |
Output isn't correct |
5 |
Correct |
108 ms |
900 KB |
Output is correct |
6 |
Correct |
71 ms |
1360 KB |
Output is correct |
7 |
Correct |
73 ms |
1668 KB |
Output is correct |
8 |
Correct |
69 ms |
1740 KB |
Output is correct |
9 |
Correct |
70 ms |
1740 KB |
Output is correct |
10 |
Correct |
70 ms |
1744 KB |
Output is correct |