input standard input
output standard output
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and boughtn colored sheets of paper, the area of each sheet is 1 square meter.
The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.
Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color.
The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
aaabbac aabbccac
6
a z
-1
In the first test sample Vasya can make an garland of area 6: he can use both sheets of colorb, three (but not four) sheets of colora and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.
In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of colorz.
题意:第一行输入的是n 种材料, 第二行输入的是要做成的花环所需的材料。可以用哈希,比如提供的材料里有4个a(只看a), 而做花环只需用3个a, 则此时用的材料的最大值为3,即从4个里直接取3个即可, 如果需用5个,则可以这样理解,先把提供的材料用掉3个,则还剩一个,但我现在还需要2个a颜色,那怎么办? 别急,题目中说可以把材料分割,那么我就可以把最后一个材料分割成2份, 那就可以满足要求了。 但如果我要用的材料在我所拥有的材料之外,那么直接输出-1即可。
#include<stdio.h>
int main()
{
int i, j, a[1001]={0}, b[1001]={0}, count = 0;
char str[1001];
gets(str);
for(i=0; str[i]!='\0'; i++)
{
a[str[i]-'a'] ++;
}
gets(str);
for(j=0; str[j]!='\0'; j++)
{
b[str[j]-'a']++;
}
for(i=0; i<26; i++)
{
if(a[i]!=0)
{
if(a[i] >= b[i])
count += b[i];
else
count += a[i];
}
else
if(b[i]!=0)
{
printf("-1\n");
return 0;
}
}
printf("%d\n", count);
return 0;
}