B. Garland

来源:互联网 发布:访客网络设置 编辑:程序博客网 时间:2024/06/05 13:23

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
aaabbacaabbccac
output
6
input
az
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.


解题说明:题目的意思是一个孩子有一大摞卡片,上面有颜色的标记,也就是输入数据的第一行aaabbac之类的,一个字母代表一种颜色。完事这个倒霉孩子想把这摞卡片整成一个颜色排列形式,也就是输入数据的第二行aabbccac之类的,一个字母表示一种颜色。问第一行和第二行相同的最大长度单位是多少?题目中有一个剪卡片操作,就是说,只要有一种颜色的卡片(比如说a)不够用了,我就可以把这个卡片剪开,这样就能当两个卡片用。所以我们只需要对每个颜色进行判断就行了。首先判断能不能成功:就是手上的颜色和目标颜色是否匹配。接着我们依次去比较相同颜色的手上的与目标的数量的大小关系。手上的多,那么一定就能搞出来,所以答案累加上目标颜色的数量;反之,我们照样能够通过剪纸的方法搞出来,只不过面积是手上的卡片的总数罢了。这样我们遍历一遍之后就能把答案搞出来。

#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include <algorithm>#include<cstring>using namespace std;int main(){int arr[26], b[26];char c;int i, cnt, f;memset(arr, 0, sizeof(arr));memset(b,0, sizeof(b));while ((c = getchar()) != '\n'){arr[c - 'a']++;}cnt = 0;while ((c = getchar()) != '\n'){b[c - 'a']++;}f = 0;for (i = 0; i<26 && !f; i++){if (b[i]>0){if (arr[i] == 0){f = 1;}else{if (b[i] >= arr[i]){cnt += arr[i];}else{cnt += b[i];}}}}if (f){printf("-1\n");}else{printf("%d\n", cnt);}return 0;}


0 0
原创粉丝点击