답안 #167580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
167580 2019-12-09T03:16:13 Z manh9203 Vještica (COCI16_vjestica) C++17
160 / 160
134 ms 1952 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
const int M = 1e5 + 5;
int n,cnt[N][30],dem[M],dp[M];
string s[N];
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> s[i];
		for(int j=0;j<s[i].length();j++){
			cnt[i][s[i][j] - 96]++;
		}
	}
	for(int mask = 1; mask < (1 << n); mask++){
		for(int i=1;i<=26;i++){
			int mn = 1e9;
			for(int j=0;j<n;j++){
				if((mask >> j) & 1){
					mn = min(mn, cnt[j+1][i]);
				}
			}
			dem[mask] += mn;
		}
	}
	dp[0] = 1e9;
	for(int mask = 1; mask < (1 << n); mask++){
		if(__builtin_popcount(mask) == 1){
			for(int i=0;i<n;i++){
				if((mask >> i) & 1){
					dp[mask] = s[i+1].length();
				}
			}
		}else{
			dp[mask] = 1e9;
			for(int sub = mask; sub > 0; sub = (sub-1) & mask){
				dp[mask] = min(dp[mask], dp[sub] + dp[sub ^ mask] - dem[mask]);
			}
		}
	}
	cout << dp[(1<<n)-1] + 1;
}

Compilation message

vjestica.cpp: In function 'int main()':
vjestica.cpp:12:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<s[i].length();j++){
               ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 130 ms 888 KB Output is correct
5 Correct 130 ms 1016 KB Output is correct
6 Correct 132 ms 1400 KB Output is correct
7 Correct 133 ms 1884 KB Output is correct
8 Correct 134 ms 1952 KB Output is correct
9 Correct 133 ms 1788 KB Output is correct
10 Correct 134 ms 1884 KB Output is correct