답안 #23056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
23056 2017-05-02T14:21:19 Z model_code Vještica (COCI16_vjestica) C++11
160 / 160
343 ms 17756 KB
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 16;
const int MAXL = 1 << 20;
const int inf = 1 << 30;

int n;
char str[MAXN][MAXL];
int cnt[MAXN][26];
int dp[1 << MAXN];

int calc_pref(int mask) {
  int len = 0;
  int tmp[26];
  fill(tmp, tmp+26, inf);
  for (int i = 0; i < n; ++i)
    if (mask&(1 << i))
      for (int j = 0; j < 26; ++j)
	tmp[j] = min(tmp[j], cnt[i][j]);

  for (int i = 0; i < 26; ++i)
    len += tmp[i];
  return len;
}


int solve(int mask) {
  int &ret = dp[mask];
  if (ret != -1) return ret;
  int pref = calc_pref(mask);
  if ((mask&-mask) == mask) return ret = pref;
  ret = inf;
  for (int i = (mask - 1) & mask; i > 0; i = (i - 1) & mask) {
    int curr = solve(i) + solve(mask ^ i) - pref;
    ret = min(ret, curr);
  }
  return ret;
}

int main (void){
  memset(dp, -1, sizeof dp);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) 
    scanf("%s", str[i]);

  for (int i = 0; i < n; ++i)
    for (int j = 0; str[i][j]; ++j)
      cnt[i][str[i][j] - 'a']++;

  printf("%d\n", solve((1 << n)-1) + 1);

  return 0;
}

Compilation message

vjestica.cpp: In function 'int main()':
vjestica.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
vjestica.cpp:51:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", str[i]);
                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17756 KB Output is correct
2 Correct 0 ms 17756 KB Output is correct
3 Correct 0 ms 17756 KB Output is correct
4 Correct 323 ms 17756 KB Output is correct
5 Correct 323 ms 17756 KB Output is correct
6 Correct 326 ms 17756 KB Output is correct
7 Correct 333 ms 17756 KB Output is correct
8 Correct 319 ms 17756 KB Output is correct
9 Correct 336 ms 17756 KB Output is correct
10 Correct 343 ms 17756 KB Output is correct