题目描述

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 Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes, please tell her the number of extra beads she has to buy; or if the answer is No, please tell her the number of beads missing from the string.For the sake of simplicity, let’s use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors.
For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.
a1092图.jpg
Figure 1

Input Specification:

Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.

Output Specification:

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

(简单的意思就是给两个字符串A和B,如果B所有字符都能在A中找到(B中如果有相同的n个字符,那么a中也要有n个对应字符才能算“找到”),则输出“Yes ”+两字符串的长度差,否则输出“No ”+没找到的字符个数)

代码实现及优化想法

其实因为字符串并不长(1000的限制),直接遍历比较就可以AC。

AC代码

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. string shop, eva;
  6. cin >> shop >> eva;
  7. int flag[1001], miss = 0, j = 0;
  8. for (int i = 0; i < shop.length(); i++)
  9. flag[i] = 0;
  10. for (int i = 0; i < eva.length(); i++)
  11. {
  12. for (j = 0; j < shop.length(); j++)
  13. if (eva[i] == shop[j] && flag[j] == 0)
  14. {
  15. flag[j]++;
  16. break;
  17. }
  18. if (j == shop.length())
  19. miss++;
  20. }
  21. if (miss)
  22. cout << "No " << miss;
  23. else
  24. cout << "Yes " << shop.length() - eva.length();
  25. return 0;
  26. }

hash优化

优化方面,可以用字符串hash的方式,复杂度可以从O(len1len2)降低到*O(len1+len2)

  1. int hashnum(char c)
  2. {
  3. if (c >= '0' && c <= '9')
  4. return c - '0';
  5. if (c >= 'a' && c <= 'z')
  6. return c - 'a' + 10;
  7. if (c >= 'A' && c <= 'Z')
  8. return c - 'A' + 36;
  9. }

之后将A字符串hash记录后,B字符串也hash,对应数组减1,小于0的时候即是没有找到。

优化后的AC代码

  1. #include <iostream>
  2. using namespace std;
  3. int hashnum(char);
  4. int main()
  5. {
  6. string shop, eva;
  7. cin >> shop >> eva;
  8. int hash[70] = {0}, miss = 0;
  9. for (int i = 0; i < shop.length(); i++)
  10. hash[hashnum(shop[i])]++;
  11. for (int i = 0; i < eva.length(); i++)
  12. {
  13. hash[hashnum(eva[i])]--;
  14. if (hash[hashnum(eva[i])] < 0)
  15. miss++;
  16. }
  17. if (miss)
  18. cout << "No " << miss;
  19. else
  20. cout << "Yes " << shop.length() - eva.length();
  21. return 0;
  22. }
  23. int hashnum(char c)
  24. {
  25. if (c >= '0' && c <= '9')
  26. return c - '0';
  27. if (c >= 'a' && c <= 'z')
  28. return c - 'a' + 10;
  29. if (c >= 'A' && c <= 'Z')
  30. return c - 'A' + 36;
  31. }