CF:Garland

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

 

解题报告:模拟题,意思是给出给出两个字符串字符串a与字符串b。每个字符串不为空由(a~z)小写的26个字母组合。然后从第一个字符串拼接出第二个字符串,字符串1中的元素可以被任意切割成多个相同的元素。问要拼出第二个字符串,最多要使用多少个字符串a的元素。

       设置两个数组c[26](第一个字串),g[26](第二个字串)。(c[i]-'a')++统计每个串的字符出现的次数,s+=min(c[i],g[i])。若g[i]中出现了c[i]中没有的元素,那么输出-1否则输出s

参考代码:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int c[26],g[26];

int main()
{
    //freopen("in.txt","r",stdin);
    int i,j,n,s;
    char cc[1001],cg[1001];
    bool flag;
   while(cin>>cc>>cg)
   {
       flag=false;
       s=0;
       memset(c,0,sizeof(c));
       memset(g,0,sizeof(g));
       n=strlen(cc);
      // cout<<n<<endl;
       for(i=0;i<n;i++)
       {
           c[cc[i]-'a']++;
       }
       n=strlen(cg);
       for(i=0;i<n;i++)
       {
           g[cg[i]-'a']++;
       }

       for(i=0;i<26;i++)
       {
           if(c[i]==0&&g[i]>0)
           {
               cout<<"-1"<<endl;
               flag=true;
               break;
           }
           else{
           if(c[i]>=g[i])
           {
               s+=g[i];
           }
           else{
               s+=c[i];
           }
           }
       }
       if(!flag)
       cout<<s<<endl;
   }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值