ZOJ - 3960 What Kind of Friends Are You? 浙江省赛 C 思维

40 篇文章 0 订阅

ZOJ - 3960 What Kind of Friends Are You?

题意:

给你 n 次询问和 q 个问题

下一行一个整数 k ,输入k个人的名字

接下来的 q 行每行包含一个整数 x 和 x 个人名,表示这x个人答对了这个问题

最后的 n 行每行输入 q 个数字(0或1),a[i]=0代表没有答对第 i 个问题,等于1代表答对,如果只有一个人满足这 q 个数字,就输出这个人名,否则输出 Let's go to the library!!。

思路:

因为n个判断只有0或1,所以可以转化为二进制的比较。每一个人对每一个问题都有一定的贡献。最后根据对问题的回答情况,确定第i(0 < i < n)个询问的贡献,查找人名中是否有唯一与之对应的

输入:

3 4
5 S R F A M
4 S R A M
1 S
1 F
1 S
1 1 0 1
0 0 0 0
1 0 0 0

输出:

S
Let's go to the library!!
Let's go to the library!!

假设问题从0开始,第0个问题,S R A M的贡献为 2^0,F的贡献为0

第1个问题,S 的贡献为 2^1 ,其他人都为0

。。。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e4+10;
int n,q;
string a;
map<string,int> mp;
int main()
{
    ios::sync_with_stdio(false);
    int t;
    int k;
    cin>>t;
    while(t--)
    {
        mp.clear();
        cin>>n>>q;
        cin>>k;
        for(int i=0;i<k;i++)
        {
            cin>>a;
            mp[a] = 0;
        }
        for(int i=0;i<q;i++)
        {
            cin>>k;
            for(int j=0;j<k;j++)
            {
                cin>>a;
                mp[a] += pow(2,i);  //计算贡献值
            }
        }

        for(int i=0;i<n;i++)
        {
            int x,cnt = 0;
            int flag = 0;
            for(int j=0;j<q;j++)
            {
                cin>>x;
                if(x==1)
                    cnt += pow(2,j);  //贡献值
            }
            map<string,int>::iterator it;
            for(it=mp.begin(); it!=mp.end(); it++)  //根据贡献值查找人
            {
                if(it->second==cnt)
                {
                    a = it->first;
                    flag++;
                }
            }
            if(flag==1)
                cout<<a<<endl;
            else
                cout<<"Let's go to the library!!"<<endl;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值