PAT Grade B 1039. Buy or Not (20)

Topic: PAT Grade B 1039. Whether to buy or not (20)

Xiaohong wants to buy some beads to make a string of beads she likes. The stall owner selling beads had many strings of colorful beads, but would not break up any of them for sale. So Xiaohong asks you to help determine whether a string of beads contains all the beads you want? 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 [0-9], [az], [AZ] to represent colors. For example, in Figure 1, the third string is the bead string that Xiaohong wants to make; then the first string can be bought because it contains all the beads she wants, and there are 8 more unnecessary beads; the second string cannot be bought , because there is no black bead and one red bead is missing.


figure 1

Input format:

Each input contains 1 test case. Each test case gives the stall owner's bead string and the bead string that Xiaohong wants to make in 2 rows, and the two strings do not exceed 1000 beads.

Output format:

If you can buy it, print "Yes" on one line and how many extra beads are there; if you can't buy it, print "No" on one line and how many beads are missing. Separate them with 1 space.

Input sample 1:
ppRYYGrrYBR2258
YrR8RrY
Sample output 1:
Yes 8
Input sample 2:
ppRYYGrrYB225
YrR8RrY
Sample output 2:
No 2


Ideas:

     It feels like the previous question 1038 is a problem of exchanging storage space for access time. First, all possible characters and numbers are stored in the array and initialized to 0. When the character is received, it is only necessary to add 1 according to the index. When using the second string to search, it is also directly searched according to the index. If the value is 0, it means that there is no lack, and the number of lacks is increased by 1. If it is found, the value is reduced by 1. In the end, if the number of missing items is not 0, "No" + the number of missing items will be output. If the missing number is 0, traverse the array to find the remaining excess number. (Once one is missing, it will be recorded as missing regardless of whether the rest are redundant).

#include<stdio.h>
#define N 1000
intmain()
{
	int Cap[26] = { 0 }, Low[26] = { 0 }, Num[10] = { 0 };
	char a[N], b[N];
	int i;
	int few = 0, more = 0;
	scanf("%s", a);
	scanf("%s", b);
	i = 0;
	while (a[i])//Scan a string
	{
		if (a[i] >= 'a'&&a[i] <= 'z')
		{
			Low[a[i] - 97]++;
		}
		else if (a[i] >= 'A'&&a[i] <= 'Z')
		{
			Cap[a[i] - 65]++;
		}
		else if (a[i] >= '0'&&a[i] <= '9')
		{
			Num[a[i] - 48]++;
		}
		i++;//Don't forget to traverse to increase
	}
	i = 0;
	while (b[i])//Scan b string
	{
		if (b[i] >= 'a'&&b[i] <= 'z')
		{
			if (Low[b[i] - 97] > 0)//If it contains
			{
				Low[b[i] - 97]--;//satisfy
			}
			else
			{
				few++;//missing
			}
		}
		else if (b[i] >= 'A'&&b[i] <= 'Z')
		{
			if (Cap[b[i] - 65] > 0)
			{
				Cap[b[i] - 65]--;
			}
			else
			{
				few++;
			}
		}
		else if (b[i] >= '0'&&b[i] <= '9')
		{
			if (Num[b[i] - 48] > 0)
			{
				Num[b[i] - 48]--;
			}
			else
			{
				few++;
			}
		}
		i++;//Don't forget to traverse to increase
	}

	if (few > 0)//missing
	{
		printf("No %d", few);
	}
	else//Excessive
	{
		for (i = 0; i < 26; i++)
		{
			if (Cap[i] != 0)
			{
				more += Cap[i];//Excessive
			}
			if (Low[i] != 0)
			{
				more += Low[i];
			}
		}
		for (i = 0; i < 10; i++)
		{
			if (Num[i] != 0)
			{
				more += Num[i];
			}
		}
		printf("Yes %d", more);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325219744&siteId=291194637