Submission #1093978

#TimeUsernameProblemLanguageResultExecution timeMemory
1093978vjudge1Vještica (COCI16_vjestica)C++17
160 / 160
1426 ms17924 KiB
#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],dp[(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; if(dp[x]!=-1) return dp[x]; return dp[x]=rec(x,0,0); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp,-1,sizeof dp); 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 (stderr)

vjestica.cpp: In function 'int main()':
vjestica.cpp:56: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]
   56 |         for(int j=0;j<s.size();j++) fre[(1<<i)][s[j]-97]++;
      |                     ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...