7-6 To Buy or Not to Buy - Hard Version (PAT ADSAA)

#include 
#include 
#include 
#include 
const int sz = 62;

int N, minNbr, extraNbr, missNbr, cnt;
std::string want;
std::vector wantCnt(sz, 0);
std::vector vec;
std::vector> vecCnt;
std::vector flag;
std::vector beads(sz);

bool cmp(const std::string &a, const std::string &b) {
  return a.size() > b.size();
}

bool judge1() {
  for (auto &c : beads) {
    c = 0;
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < sz; ++j) {
      beads[j] += vecCnt[i][j];
    }
  }
  bool ff = true;
  missNbr = 0;
  for (int i = 0; i < sz; ++i) {
    if (wantCnt[i] > beads[i]) {
      missNbr += wantCnt[i] - beads[i];
      ff = false;
    } else {
      minNbr += beads[i] - wantCnt[i];
    }
  }
  return ff;
}

bool judge2(int k, int &eNbr) {
  bool ff = true;
  for (int i = 0; i < sz; ++i) {
    if (wantCnt[i] > 0) {
      ff = false;
    } else {
      eNbr += -wantCnt[i];
    }
  }
  if (ff && eNbr < minNbr) {
    minNbr = eNbr;
  }
  return ff;
}

void addString(int k) {
  for (int i = 0; i < sz; ++i) {
    wantCnt[i] -= vecCnt[k][i];
  }
}

void restore(int k) {
  for (int i = 0; i < sz; ++i) {
    wantCnt[i] += vecCnt[k][i];
  }
}

void backtracking(int k, int fflag) {
  extraNbr = 0;
  if (fflag == 1) {
    if (judge2(k, extraNbr) || extraNbr >= minNbr) {
      return;
    }
  }
  if (0 == k) {
    return;
  }
  addString(k - 1);
  backtracking(k - 1, 1);
  restore(k - 1);
  backtracking(k - 1, -1);
}

void charCnt(std::string a, std::vector &cnt) {
  for (int i = 0; i < a.size(); ++i) {
    if (a[i] >= '0' && a[i] <= '9') {
      ++cnt[a[i] - '0'];
    } else if (a[i] >= 'a' && a[i] <= 'z') {
      ++cnt[a[i] - 'a' + 10];
    } else {
      ++cnt[a[i] - 'A' + 36];
    }
  }
}

int main() {
  std::cin >> want;
  charCnt(want, wantCnt);
  std::cin >> N;
  vec.resize(N);
  vecCnt.resize(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> vec[i];
    vecCnt[i].resize(sz, 0);
  }
  sort(vec.begin(), vec.end(), cmp);
  for (int i = 0; i < N; ++i) {
    charCnt(vec[i], vecCnt[i]);
  }
  if (!judge1()) {
    std::cout << "No"
              << " " << missNbr;
  } else {
    addString(N - 1);
    backtracking(N - 1, 1);
    restore(N - 1);
    backtracking(N - 1, -1);
    std::cout << "Yes"
              << " " << minNbr;
  }
  return 0;
}

题目如下:

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence in some cases Eva might have to buy several strings to get all the beads she needs. With a hundred strings in the shop, Eva needs your help to tell her whether or not she can get all the beads she needs with the least number of extra beads she has to pay for.

For the sake of simplicity, let's use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. In sample 1, buying the 2nd and the last two strings is the best way since there are only 3 extra beads. In sample 2, buying all the three strings won't help since there are three R beads missing.

Input Specification:

Each input file contains one test case. Each case first gives in a line the string that Eva wants. Then a positive integer N (≤100) is given in the next line, followed by N lines of strings that belong to the shop. All the strings contain no more than 1000 beads.

Output Specification:

For each test case, print your answer in one line. If the answer is Yes, then also output the least number of extra beads Eva has to buy; or if the answer is No, then also output the number of beads missing from all the strings. There must be exactly 1 space between the answer and the number.

Sample Input 1:

RYg5
8
gY5Ybf
8R5
12346789
gRg8h
5Y37
pRgYgbR52
8Y
8g

Sample Output 1:

Yes 3

Sample Input 2:

YrRR8RRrY
3
ppRGrrYB225
8ppGrrB25
Zd6KrY

Sample Output 2:

No 3

你可能感兴趣的:(PAT,ADSAA,c++,算法,pat考试)