#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
int common[(1<<16)+1],fre[(1<<16)+1][30],vis[(1<<16)+1],fre2[(1<<16)+1];
int n;
int count(int x){
int cnt=0;
for(int i=0;i<=16;i++) if((bool)((1<<i)&x)!=0) cnt++;
return cnt;
}
void build(int x){
if(vis[x]==1||count(x)==1) return;
vis[x]=1;
int num=x,j=0;
for(int bt=0;bt<=16;bt++){
if((bool)((1<<bt)&x)!=0){
num^=(1<<bt);
j=bt;
break;
}
}
build(num);
int sum=0;
for(int i=0;i<30;i++){
fre[x][i]=min(fre[(1<<j)][i],fre[num][i]);
sum+=fre[x][i];
}
common[x]=sum;
return;
}
int solve(int x);
int rec(int x,int num,int i){
if(i==n){
if(x!=num&&num!=0) return solve(num)+solve(x^num)-common[x]*2+common[num]+common[x^num];
else return 1e15;
}
if((bool)(x&(1<<i))==0) return rec(x,num,i+1);
else return min(rec(x,num,i+1),rec(x,(int)(num|(1<<i)),i+1));
}
int solve(int x){
if(count(x)==1) return 0;
return rec(x,0,0);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<s.size();j++) fre[(1<<i)][s[j]-97]++;
common[(1<<i)]=s.size();
}
for(int i=1;i<=(1<<n)-1;i++) build(i);
//cout<<(1<<n)-1<<" "<<common[6]<<endl;
cout<<solve((1<<n)-1)+common[(1<<n)-1]+1<<endl;
return 0;
}
Compilation message
vjestica.cpp: In function 'int main()':
vjestica.cpp:54:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j=0;j<s.size();j++) fre[(1<<i)][s[j]-97]++;
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2037 ms |
4444 KB |
Time limit exceeded |
2 |
Execution timed out |
2070 ms |
4444 KB |
Time limit exceeded |
3 |
Execution timed out |
2068 ms |
4444 KB |
Time limit exceeded |
4 |
Execution timed out |
2070 ms |
16988 KB |
Time limit exceeded |
5 |
Execution timed out |
2062 ms |
17244 KB |
Time limit exceeded |
6 |
Execution timed out |
2065 ms |
17500 KB |
Time limit exceeded |
7 |
Execution timed out |
2061 ms |
17496 KB |
Time limit exceeded |
8 |
Execution timed out |
2041 ms |
17752 KB |
Time limit exceeded |
9 |
Execution timed out |
2076 ms |
17492 KB |
Time limit exceeded |
10 |
Execution timed out |
2072 ms |
17500 KB |
Time limit exceeded |