What to Cook?

题目描述

The renowned restaurant Gusteau’s has closed down, and now Remy is working at a new restaurant called Highly Sophisticated Parisian Cuisine (HSPC). However, business is busy, and Remy needs help determining whether he can cook the various dishes that patrons are asking for.
Given a list of ingredients on hand and an ordered list of recipes the guests are asking for, state if a recipe can be cooked with the ingredients left.
If an earlier recipe uses up a given ingredient, then that ingredient is no longer available to successive recipes. For example, assume there are 5 slices of bread in the inventory, the first recipe uses 4 of them,and the second uses 3. The first recipe could be made, but not the second, since there is only 1 slice of bread left over after the first recipe is made. If a recipe can not be made, then there are no ingredients removed for that given recipe. All recipies should be considered in the order provided.

输入

The first line of input will be a single integer n ≤ 200. There will be n test cases that follow.
Each test case will begin with a line with two space-separated integers: 1 ≤ i ≤ 75 and 1 ≤ r ≤ 75. The i is for how many items are in stock in the inventory, and r represents how many recipies will be specified. The inventory will be represented by the following i lines; each line will each hold one ingredient. Each line will contain the ingredient count (a positive integer) and the ingredient (an alphanumeric string). Any given ingredient will only be listed once per test case in the inventory.
r recipes will follow. Each recipe begins with a line consisting of an integer l ≤ 10, the number of ingredients for that recipe. Each of the following l lines will hold one required ingredient for the recipe (and the quantity), represented as they were in the inventory specification. An ingredient not listed in the inventory may be listed in a recipe. Any given ingredient will only be listed once for any given recipe.

输出

For each test case, first output the test case number x as “Case x:” (start from 1 and don’t forget the colon!) on its own line. Then, iterate through the recipes in that test case; output “Yes” if it can be made with the ingredients remaining or “No” if it cannot. There should be no blank lines between test cases.

样例输入

1
5 3
1 apples
3 onions
2 eggs
5 beans
1 pepper
3
1 apples
1 onions
4 beans
1
2 beans
1
1 oranges

样例输出

Case 1:
Yes
No
No

思路:题目要求很简单,但是需要注意的点很多。判断某种食物是否可以被制作成功,就判断是否有这种食材,还有判断食材的量是否足够。判断结束可以制作成功再去减掉食物的量,要不就会导致食材数量混乱。每一组数据map需要初始化。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

map<string,int> m;
int main()
{
    int n,i,r,v[20],y;
    string s[20];
    scanf("%d",&n);
    for(int q=1;q<=n;q++){
        printf("Case %d:\n",q);
        m.clear();
        scanf("%d %d",&i,&r);
        for(int t=0;t<i;t++){
            cin>>v[0]>>s[0];
            m[s[0]]=v[0];
        }
        for(int t=0;t<r;t++){
            scanf("%d",&y);
            int flag=0;
            for(int j=0;j<y;j++){
                cin>>v[j]>>s[j];
                if(m.count(s[j])){//m.count()判断s[j]食材是否存在
                    if(m[s[j]]>=v[j]){
                        continue;
                    }
                    else{
                        flag=1;
                    }
                }
                else{
                    flag=1;
                }
            }
            if(flag==0){//成立再减食材
                printf("Yes\n");
                for(int j=0;j<y;j++){
                    m[s[j]]-=v[j];
                }
            }
            else{
                printf("No\n");
            }
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值