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