ZOJ 3960 What Kind of Friends Are You?(读题+思维)
Posted Reqaw’s Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 3960 What Kind of Friends Are You?(读题+思维)相关的知识,希望对你有一定的参考价值。
题目链接
:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5592
Japari Park is a large zoo home to extant species, endangered species, extinct species, cryptids and some legendary creatures. Due to a mysterious substance known as Sandstar, all the animals have become anthropomorphized into girls known as Friends.
Kaban is a young girl who finds herself in Japari Park with no memory of who she was or where she came from. Shy yet resourceful, she travels through Japari Park along with Serval to find out her identity while encountering more Friends along the way, and eventually discovers that she is a human.
However, Kaban soon finds that it‘s also important to identify other Friends. Her friend, Serval, enlightens Kaban that she can use some questions whose expected answers are either "yes" or "no" to identitfy a kind of Friends.
To be more specific, there are n Friends need to be identified. Kaban will ask each of them q same questions and collect their answers. For each question, she also gets a full list of animals‘ names that will give a "yes" answer to that question (and those animals who are not in the list will give a "no" answer to that question), so it‘s possible to determine the name of a Friends by combining the answers and the lists together.
But the work is too heavy for Kaban. Can you help her to finish it?
Input
There are multiple test cases. The first line of the input is an integer T (1 ≤ T ≤ 100), indicating the number of test cases. Then T test cases follow.
The first line of each test case contains two integers n (1 ≤ n ≤ 100) and q (1 ≤ q ≤ 21), indicating the number of Friends need to be identified and the number of questions.
The next line contains an integer c (1 ≤ c ≤ 200) followed by c strings p1, p2, ... , pc (1 ≤ |pi| ≤ 20), indicating all known names of Friends.
For the next q lines, the i-th line contains an integer mi (0 ≤ mi ≤ c) followed by mi strings si, 1, si, 2, ... , si, mi (1 ≤ |si, j| ≤ 20), indicating the number of Friends and their names, who will give a "yes" answer to the i-th question. It‘s guaranteed that all the names appear in the known names of Friends.
For the following n lines, the i-th line contains q integers ai, 1, ai, 2, ... , ai, q (0 ≤ ai, j ≤ 1), indicating the answer (0 means "no", and 1 means "yes") to the j-th question given by the i-th Friends need to be identified.
It‘s guaranteed that all the names in the input consist of only uppercase and lowercase English letters.
Output
For each test case output n lines. If Kaban can determine the name of the i-th Friends need to be identified, print the name on the i-th line. Otherwise, print "Let‘s go to the library!!" (without quotes) on the i-th line instead.
Sample Input
2 3 4 5 Serval Raccoon Fennec Alpaca Moose 4 Serval Raccoon Alpaca Moose 1 Serval 1 Fennec 1 Serval 1 1 0 1 0 0 0 0 1 0 0 0 5 5 11 A B C D E F G H I J K 3 A B K 4 A B D E 5 A B K D E 10 A B K D E F G H I J 4 B D E K 0 0 1 1 1 1 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 1
Sample Output
Serval Let‘s go to the library!! Let‘s go to the library!! Let‘s go to the library!! Let‘s go to the library!! B Let‘s go to the library!! K
Hint
The explanation for the first sample test case is given as follows:
As Serval is the only known animal who gives a "yes" answer to the 1st, 2nd and 4th question, and gives a "no" answer to the 3rd question, we output "Serval" (without quotes) on the first line.
As no animal is known to give a "no" answer to all the questions, we output "Let‘s go to the library!!" (without quotes) on the second line.
Both Alpaca and Moose give a "yes" answer to the 1st question, and a "no" answer to the 2nd, 3rd and 4th question. So we can‘t determine the name of the third Friends need to be identified, and output "Let‘s go to the library!!" (without quotes) on the third line.
1 /* 2 问题 3 第一个表输入每个人给出每个问题回答,第一个集合中出现的是1,没有出现的是0 4 第二个表输入需要识别的每个人对于每个问题的回答,结合两张表,看是否存在唯一一个人, 5 是输出他的名字,否输出Let‘s go to the library!! 6 7 解题思路 8 先将每个人根据名字进行编号,使用map映照容器更方便,然后制作两张表,两重循环查询即可。 9 */ 10 #include<cstdio> 11 #include<cstring> 12 #include<string> 13 #include<iostream> 14 #include<map> 15 using namespace std; 16 17 void f(int h); 18 int n,q,c; 19 map<string,int> namelist; 20 int list1[200][30],list2[200][30]; 21 int OK(int x[],int y[]); 22 23 int main() 24 { 25 int t,i,j,k,m; 26 string sname; 27 char name[30]; 28 scanf("%d",&t); 29 while(t--){ 30 scanf("%d%d%d",&n,&q,&c); 31 namelist.clear(); 32 for(i=0;i<c;i++){ 33 scanf("%s",name); 34 sname=name; 35 namelist[sname]=i; 36 } 37 38 memset(list1,0,sizeof(int)*200*30); 39 for(i=0;i<q;i++){ 40 scanf("%d",&m); 41 for(j=0;j<m;j++){ 42 scanf("%s",name); 43 sname=name; 44 list1[ namelist[sname] ][i]=1; 45 } 46 } 47 48 /*for(i=0;i<c;i++){ 49 for(j=0;j<q;j++){ 50 printf("#%d ",list1[i][j]); 51 } 52 printf("\n"); 53 }*/ 54 55 for(i=0;i<n;i++){ 56 for(j=0;j<q;j++){ 57 scanf("%d",&list2[i][j]); 58 } 59 } 60 61 /*for(i=0;i<n;i++){ 62 for(j=0;j<q;j++){ 63 printf("@%d ",list2[i][j]); 64 } 65 printf("\n"); 66 }*/ 67 68 for(i=0;i<n;i++){ 69 f(i); 70 } 71 } 72 return 0; 73 } 74 75 int OK(int x[],int y[]) 76 { 77 int i; 78 for(i=0;i<q;i++){ 79 if(x[i] != y[i]) 80 return 0; 81 } 82 return 1; 83 } 84 85 void f(int h) 86 { 87 int i,j,k; 88 int cou=0,z=0; 89 for(i=0;i<c;i++){ 90 if(OK(list1[i],list2[h])){ 91 z=i; 92 cou++; 93 } 94 } 95 96 if(cou == 1){ 97 map<string,int>::iterator it; 98 for(it=namelist.begin();it != namelist.end();it++){ 99 if((*it). second == z){ 100 cout<<(*it).first<<endl; 101 break; 102 } 103 } 104 } 105 else 106 printf("Let‘s go to the library!!\n"); 107 }
以上是关于ZOJ 3960 What Kind of Friends Are You?(读题+思维)的主要内容,如果未能解决你的问题,请参考以下文章
ZOJ 3960: What Kind of Friends Are You?
2017浙江省赛 C - What Kind of Friends Are You? ZOJ - 3960
ZOJ What Kind of Friends Are You?