题目概述:
这道题其实就像玩“猜猜我是谁”的游戏。已知一些Friends对问题的回答情况,现在有一个新的 Friend,它也回答了这些问题。我们要判断它是不是某个已知的 Friend。如果是唯一的可能,就输出名字;否则,就输出“Let’s go to the library!!”。
解析:
集合存储“yes”回答,用集合存储每个问题“yes”的 Friend 名称,这样可以快速判断某个 Friend 是否回答了“yes”。就像有个名单,一查便知。
然后逐一匹配,对于每个未知 Friend ,遍历所有已知 Friends ,逐个问题判断是否匹配。这有点像挨个比对嫌疑人的特征,看谁和描述一致。
最后处理特殊情况,如果没有匹配的,说明这个 Friend 不在已知名单里;如果有多个匹配的,那我们无法确定到底是谁。
代码实现
同解析,分三步走。
读取输入:先读取测试用例数量,然后依次处理每个测试用例。读取已知 Friends 名称和每个问题对应的“yes”回答列表,存入集合中。
匹配过程:对于每个未知 Friend ,遍历每个问题的回答,和已知Friends的特性进行比较。如果某个已知 Friend 完全匹配,就记录下来。
判断输出:根据匹配结果,判断是输出对应名字还是那句“去图书馆”的话。
AC Code:
#include<bits/stdc++.h>
using namespace std;
const int maxc=210;
const int maxs=30;
int T,n,q,c,m,a[maxs];
string s[maxc];
set<string> st[maxs];
int main(){
cin>>T;
while(T--){
cin>>n>>q>>c;
for(int i=0;i<c;i++){
cin>>s[i];
}
for(int i=0;i<q;i++){
st[i].clear();
cin>>m;
while(m--){
string t;
cin>>t;
st[i].insert(t);
}
}
while(n--){
for(int i=0;i<q;i++){
cin>>a[i];
}
string r="";
int cnt=0;
for(int i=0;i<c;i++){
int f=1;
for(int j=0;j<q;j++){
if(st[j].count(s[i])!=a[j]){
f=0;
}
}
if(f){
cnt++;
r=s[i];
}
}
if(cnt==1){
cout<<r<<endl;
}else{
cout<<"Let's go to the library!!"<<endl;
}
}
}
return 0;
}✿✿ヽ(゚▽゚)ノ✿完结撒花