• Codeforces Round #239 --B



                                                           B. Garland
      time limit per test     1 second           memory limit per test      256 megabytes
      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.

    Input

    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.

    Output

    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.

    Sample test(s)
    Input
    aaabbac
    aabbccac
    
    Output
    6
    
    Input
    a
    z
    
    Output
    -1
    Note

    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]!=''; i++)
    	{
    		 a[str[i]-'a'] ++;
    	}
    	gets(str);
    	for(j=0; str[j]!=''; 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
    ");
    				return 0;
    			}
    	}
    	printf("%d
    ", count);
    return 0;
    }

    每天训练发现我比别人做的好慢,但是理解的更深刻,如果一开始学一个新知识点就搜模板,那么这样的人是走不远的,毕业之后带走的只有思维,什么荣誉,奖杯都已经不重要了。
  • 相关阅读:
    php 魔术方法
    有用的函数系统采集(一)
    php后期静态绑定
    php对象引用及序列化
    php文件操作
    复习mysql视图总结
    jQuery调用WebService详解
    这个网站的界面描述甚好,我喜欢
    喜欢和技术有关的一切~
    Introduction
  • 原文地址:https://www.cnblogs.com/6bing/p/3931253.html
Copyright © 2020-2023  润新知