`
Simone_chou
  • 浏览: 185632 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Garland(模拟)

    博客分类:
  • CF
 
阅读更多
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 bought n 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 color b, three (but not four) sheets of color aand 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 color z.

 

      题意:

      给出 N (1 ~ 1000)和 M (1 ~ 1000)两个字符串,N 中每个字母代表一种原材料颜色,可以将每种颜色任意分成N部分,需要用 N 构成 M,输出最多 N 中多少的颜色数量。若不能构成 M ,则输出 -1。

 

      思路:

      模拟。脑残又读错了题。问的是最多需要N中的颜色数量,误解成最多能构成 M 中多长的颜色块,必须要构成全部的 M ,而不是一部分 M 。

 

      AC:

#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;

int main() {
        char s1[1005], s2[1005];
        int c1[30], c2[30];

        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));

        scanf("%s%s", s1, s2);

        for (int i = 0; i < strlen(s1); ++i)
                c1[s1[i] - 'a']++;

        for (int i = 0; i < strlen(s2); ++i)
                c2[s2[i] - 'a']++;

        int sum = 0;
        for (int i = 0; i < 26; ++i) {
                if(c2[i] && !c1[i]) {
                        sum = -1;
                        break;
                }
                sum += min(c2[i],c1[i]);
        }

        printf("%d\n",sum);

        return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics