// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
//#include "bits/stdc++.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"---------------------------\n"
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
a+=b;
a+=mod;
if(a>=mod) a%=mod;
return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 17
#define maxc 30
#define maxx ((1<<17)+5)
ll cnt[maxn][maxc];
ll a[maxn];
ll dp[maxx];
ll n;
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n;
for(ll i = 0;i<n;i++){
string s; cin >> s;
for(char c : s) cnt[i][c-'a']++;
a[i] = sz(s);
}
for(ll i = 0;i<n;i++){
dp[1<<i] = a[i];
}
for(ll i = 0;i<(1<<n);i++){
if(__builtin_popcountll(i)==1) continue;
ll len = 0;
for(ll c = 0;c<27;c++){
ll leni = llinf;
for(ll j = 0;j<n;j++){
if(((1<<j)&i)==0) continue;
leni = min(leni,cnt[j][c]);
}
len+=leni;
}
dp[i] = llinf;
for(ll mask = i;mask>0;mask = i&(mask-1)){
if(i==mask) continue;
ll mask2 = i^mask;
dp[i] = min(dp[i],dp[mask]+dp[mask2]-len);
}
}
cout<<dp[(1<<n)-1]+1<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
vjestica.cpp: In function 'void setIO(std::string)':
vjestica.cpp:67:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vjestica.cpp:68:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
181 ms |
820 KB |
Output is correct |
5 |
Correct |
172 ms |
896 KB |
Output is correct |
6 |
Correct |
173 ms |
1080 KB |
Output is correct |
7 |
Correct |
178 ms |
1252 KB |
Output is correct |
8 |
Correct |
175 ms |
1324 KB |
Output is correct |
9 |
Correct |
190 ms |
1332 KB |
Output is correct |
10 |
Correct |
182 ms |
1224 KB |
Output is correct |