题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805283241443328
柳:https://www.liuchuo.net/archives/571

思路

建立一个哈希表就完事了
或者直接vector hashtable(256)更简单,也别做什么映射了,可参考柳的解法

代码

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6. vector<int> hashtable(62,0);
  7. int main(){
  8. string c;
  9. bool flag = false;
  10. cin>>c;
  11. for(int i = 0; i < c.size(); i++){
  12. if(c[i] <= 'z'&&c[i] >= 'a') hashtable[c[i] -'a']++;
  13. else if(c[i] <= 'Z'&&c[i] >= 'A') hashtable[c[i] -'A'+26]++;
  14. else if(c[i] <= '10' &&c[i] >= '0') hashtable[c[i] - '0' + 52]++;
  15. }
  16. cin>>c;
  17. for(int i = 0; i < c.size(); i++){
  18. if(c[i] <= 'z'&&c[i] >= 'a') hashtable[c[i] -'a']--;
  19. else if(c[i] <= 'Z'&&c[i] >= 'A') hashtable[c[i] -'A'+26]--;
  20. else if(c[i] <= '10' &&c[i] >= '0') hashtable[c[i] - '0' + 52]--;
  21. }
  22. for(int i = 0; i < 62; i++)
  23. {
  24. if(hashtable[i]<0) flag = true;
  25. }
  26. int outNum = 0;
  27. //如果有小于0的
  28. if(flag)
  29. {
  30. for(int i = 0; i < 62; i++){
  31. if(hashtable[i]<0)
  32. outNum -= hashtable[i];
  33. }
  34. printf("No %d",outNum);
  35. }
  36. else{
  37. for(int i = 0; i < 62; i++){
  38. outNum += hashtable[i];
  39. }
  40. printf("Yes %d",outNum);
  41. }
  42. }