CodeForces - 408B

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.

Examples
input
aaabbac
aabbccac
output
6
input
a
z
output
-1
解题思路:
统计所有的字母个数,分三种情况:
1.如果字符串s2有而字符串s1没有,直接输出-1
2.如果字符串s2中字母个数对应s1中的字母个数大则取s1中字母个数
3.如果字符串s2中字母个数对应s1中的字母个数小则取s2中的字母个数

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){
	char s[1010],s1[1010];
	int a[30],a1[30];
	int len,len1;
	
	scanf("%s%s",s,s1);
		len=strlen(s);
		len1=strlen(s1);
		memset(a, 0, sizeof(a));  
        memset(a1, 0, sizeof(a1));  
		for(int i=0;i<len;i++){
			a[s[i]-'a']++;
		}		
		for(int i=0;i<len1;i++){
			a1[s1[i]-'a']++;
		}
		int sum=0,flag=1;
		for(int i=0;i<26;i++){
			if(a1[i]!=0&&a[i]==0){
				printf("-1\n");
				flag=0;
				break;
			}	
			if(a1[i]<=a[i]){
				sum+=a1[i];
			}
			else{
				sum+=a[i];
			}			
		}
		if(flag){
			printf("%d\n",sum);
		}
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值