跳到正文

题解

P14102 [ZJCPC 2017] What Kind of Friends Are You? 题解

Mss1617约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月21日
篇幅
约 3 分钟
题目
P14102

版本

  1. v3当前
  2. v2
  3. v1

题目概述

这道题其实就像玩“猜猜我是谁”的游戏。已知一些 Friends 对问题的回答情况,现在有一个新的 Friend,它也回答了这些问题。我们要判断它是不是某个已知的 Friend。如果是唯一的可能,就输出名字;否则,就输出Let's go to the library!!。


题目分析

集合存储yes回答,用集合存储每个问题yes的 Friend 名称,这样可以快速判断某个 Friend 是否回答了yes。就像有个名单,一查便知。

然后逐一匹配,对于每个未知 Friend,遍历所有已知 Friends,逐个问题判断是否匹配。这有点像挨个比对嫌疑人的特征,看谁和描述一致。

最后处理特殊情况,如果没有匹配的,说明这个 Friend 不在已知名单里;如果有多个匹配的,那我们无法确定到底是谁。


代码实现

同解析,分三步走。

读取输入:先读取测试用例数量,然后依次处理每个测试用例。读取已知 Friends 名称和每个问题对应的yes回答列表,存入集合中。

匹配过程:对于每个未知 Friend,遍历每个问题的回答,和已知 Friends 的特性进行比较。如果某个已知 Friend 完全匹配,就记录下来。

判断输出:根据匹配结果,判断是输出对应名字还是Let's go to the library!!。


满分代码

C++
#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;
}
原文