Interesting question: whether to buy or not. Interesting question: whether to buy or not

Source: Internet
Author: User

Interesting question: whether to buy or not. Interesting question: whether to buy or not

Description: Xiaohong wants to buy some beads to make a string of his favorite beads. The vendors selling beads have a lot of colorful beads, but they are not willing to split up and sell any of them. So Xiaohong asked you to help determine if all the beads you wanted were contained in a beaded sub-table? If yes, tell her how many extra beads are there; if not, tell her how many beads are missing.

For convenience, we use characters in the range of [0-9], [a-z], [A-Z] to represent the color. For example, in figure 1, the 3rd string is the beaded string that Xiaohong wants to make; then the 1st string can be bought because it contains all the beads she wants, and 8 unnecessary beads are added; 2nd strings cannot be bought, because there is no black beads and a red bead is missing.

 

Input: each input contains one test case. In each test case, the beaded string of the stall master and the beaded string of the small red wish are successively given in two rows, each of which contains no more than 1000 beads.

Output: If you can buy one, "Yes" and how many extra beads are output in one row. If you cannot buy one, "No" and how many beads are missing in one row are output. Separated by one space.

Input: ppRYYGrrYBR2258

YrR8RrY

Output:Yes8

Analysis: the difficulty of this question is that each beaded may have multiple of the same colors, and you need to determine the number of the same colors.

The solution is to use an array to store the number of beads of each color and then compare them.

1 # include <iostream> 2 # include <string> 3 using namespace std; 4 5 void count (string a, int num []) // store the number of beads of different colors in the array 6 {7 for (int I = 0; I <. size (); I ++) 8 {9 if (a [I]> = '0' & a [I] <= '9 ') 10 num [a [I]-'0'] ++; 11 else if (a [I]> = 'A' & a [I] <= 'Z ') 12 num [a [I]-'A' + 10] ++; 13 else14 num [a [I]-'A' + 35] ++; 15} 16} 17 int main () 18 {19 string a, B; 20 static int nA [62], nB [62]; 21 cin> a> B; 22 count (a, nA); 23 count (B, nB); 24 int n = 0; // The number of missing beads is written in n. If n = 0, note: No. You can buy 25 for (int I = 0; I <62; I ++) 26 {27 if (nB [I] = 0) 28 continue; 29 if (nB [I]> nA [I]) 30 n + = nB [I]-nA [I]; 31} 32 if (n = 0) 33 cout <"Yes" <"" <. size ()-B. size (); 34 else35 cout <"No" <"" <n <endl; 36 system ("pause"); 37 return 0; 38}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.