codeforces 408B Garland

来源:互联网 发布:linux域名绑定ip 编辑:程序博客网 时间:2024/06/01 08:23

地址: http://codeforces.com/problemset/problem/408/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 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
每个颜色必须都拥有,求最大而不是什么连续最大

代码:

#include <stdio.h>#include <string.h>int main(){char n[1005];char m[1005];int N[26];int M[26];while (scanf("%s%s", n, m) != EOF){int len_n = strlen(n);int len_m = strlen(m);memset(N, 0, sizeof(N));memset(M, 0, sizeof(M));for (int i = 0; i < len_n; i++)N[n[i] - 'a']++;for (int i = 0; i < len_m; i++)M[m[i] - 'a']++;int sign = 1, ans = 0;for (int i = 0; i < 26; i++){if (0 == N[i] && 0 != M[i]){sign = 0;puts("-1");break;}else if (N[i] < M[i])ans += N[i];elseans += M[i];}if (sign)printf("%d\n", ans);}return 0;}





0 0
原创粉丝点击