codeforces problem/408/B


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
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.

要求:第二个字符串中有的字母必须都有,两个字符串中都有的字符串去个数最少的那个的个数计数。

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char a[10005],b[10005];
int num[10005],num1[10005];
int main()
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    int sum,n,m,flag;
    while(~scanf("%s%s",a,b))
    {
        n=strlen(a);
        m=strlen(b);
        for(int i=0; i<m; i++)
        {
            flag=0;
            for(int j=0; j<n; j++)
            {
                if(b[i]==a[j])
                    flag=1;
            }
            if(flag==0)
            {
                printf("-1\n");
                break;
            }
        }
        sum=0;
        if(flag)
        {
            for(int i=0; i<n; i++)
                num[a[i]-'a']++;
            for(int i=0;i<m;i++)
                num1[b[i]-'a']++;
            for(int i=0; i<27; i++)
                sum+=min(num[i],num1[i]);
            printf("%d\n",sum);
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            memset(num1,0,sizeof(num1));
            memset(num,0,sizeof(num));
        }
    }
    return 0;
}


你可能感兴趣的:(codeforces problem/408/B)