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.
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.
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.
aaabbac aabbccac
6
a z
-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;
}