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