codeores--B. Garland--3.30

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 a and 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>
int main()
{
    int i , j , p[28] , q[28] , m , l ;
    char s1[1002] , s2[1002] ;
    scanf("%s %s", s1, s2);
    memset(p,0,sizeof(p));
    memset(q,0,sizeof(q));
    for(i = 0 ; s1[i] != '\0' ; i++)
        p[ s1[i]-'a' ]++ ;
    for(j = 0 ; s2[j] != '\0' ; j++)
        q[ s2[j]-'a' ]++ ;
    for(i = 0 ; i < 26 ; i++)
    {
        if(p[i] > q[i])
            p[i] = q[i] ;
        q[i] = p[i] ;
    }
    m = 0 ; l = 0 ;
    for(i = 0 ; s2[i] != '\0' ; i++)
    {
        int k = s2[i] - 'a' ;
        if( q[k] )
        {
            if(p[k])
            {
                l++ ;
                p[k]--;
            }
        }
        else break;
    }
    if(l > m)
        m = l ;
    if(s2[i] == '\0' && m != 0)
        printf("%d\n", m);
    else
        printf("-1\n");
    return 0;
}


你可能感兴趣的:(codeores--B. Garland--3.30)