Garland(模拟)

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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值