当前位置: 代码迷 >> 综合 >> Codeforces Round #239 (Div. 2) B. Garland(字符串统计题目,简单题)
  详细解决方案

Codeforces Round #239 (Div. 2) B. Garland(字符串统计题目,简单题)

热度:10   发布时间:2024-01-13 20:17:04.0

1、http://codeforces.com/contest/408/problem/B

2、题目大意:

现在有两个字符串,每个字符代表一张1平方米这种颜色的纸,现在要拼成第二个字符串表示的纸张,求是否能拼成,不能输出-1,能输出拼成的最大面积是多少

题目不难

分别统计出两个字符串有多少个字母及每个字母的个数,

比较即可,如果a中没有的,b中有,那么肯定是不能,输出-1

如果a中的该字符比b中的多,按b中的计算,如果a中的比b中的少,按照a中的计算

3、题目:

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.

4、Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char sa[1005];
char sb[1005];
int a[30],b[30];
int main()
{while(scanf("%s%s",sa,sb)!=EOF){int la=strlen(sa);int lb=strlen(sb);memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(int i=0;i<la;i++){a[sa[i]-'a']++;}for(int i=0;i<lb;i++){b[sb[i]-'a']++;}int sum=0,flag=0;for(int i=0;i<26;i++){if(a[i]==0 && b[i]!=0){flag=1;printf("-1\n");break;}if(a[i]!=0 && b[i]!=0){if(a[i]<b[i])sum+=a[i];elsesum+=b[i];}}if(flag==0)printf("%d\n",sum);}return 0;
}
/*
abc
abcaac
aadaad
aad
*/


  相关解决方案