Submission #456867

# Submission time Handle Problem Language Result Execution time Memory
456867 2021-08-07T03:01:20 Z ntabc05101 Vještica (COCI16_vjestica) C++14
160 / 160
114 ms 1044 KB
#include<bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

#define taskname ""

int main() {
  if (fopen(taskname".inp", "r")) {
    freopen(taskname".inp", "r", stdin);
    freopen(taskname".out", "w", stdout);
  }

  cin.tie(0)->sync_with_stdio(0);

  int n; cin >> n;
  int c[n][26];
  memset(c, 0, sizeof(c));
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (auto& _: s) {
      c[i][_ - 'a']++;
    }
  }

  const int full_bit = 1 << n;

  int dp[full_bit];
  dp[0] = 0;
  for (int msk = 1; msk < full_bit; msk++) {
    int n_node = 0;
    vector<int> _min(26, mod);
    for (int i = 0; i < n; i++) {
      if (msk >> i & 1) {
        for (int j = 0; j < 26; j++) {
          _min[j] = min(_min[j], c[i][j]);
        }
      }
    }
    for (int i = 0; i < 26; i++) {
      n_node += _min[i];
    }

    if ((msk & -msk) == msk) {
      dp[msk] = n_node;
    }
    else {
      dp[msk] = mod;
      for (int _msk = (msk - 1) & msk; _msk; _msk = (_msk - 1) & msk) {
        dp[msk] = min(dp[msk], dp[_msk] + dp[msk ^ _msk] - n_node);
      }
    }
  }

  //cout << dp[2] << "\n";
  cout << dp[full_bit - 1] + 1 << endl;

  return 0;
}

Compilation message

vjestica.cpp: In function 'int main()':
vjestica.cpp:10:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |     freopen(taskname".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
vjestica.cpp:11:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |     freopen(taskname".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 104 ms 460 KB Output is correct
5 Correct 105 ms 644 KB Output is correct
6 Correct 114 ms 856 KB Output is correct
7 Correct 105 ms 1024 KB Output is correct
8 Correct 106 ms 980 KB Output is correct
9 Correct 109 ms 1044 KB Output is correct
10 Correct 108 ms 972 KB Output is correct