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 ≤ mic) 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 }

原文地址:https://www.cnblogs.com/wenzhixin/p/9001951.html

时间: 2024-10-07 10:35:10

ZOJ 3960 What Kind of Friends Are You?(读题+思维)的相关文章

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

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3960 题目: 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, a

ZOJ 3960: What Kind of Friends Are You?

What Kind of Friends Are You? ///@author Sycamore, ZJNU ///@date 4/22/2017 #include <iostream> #include <sstream> #include <iomanip> #include <cmath> #include <string> #include <algorithm> #include <numeric> #incl

zoj 3960 What Kind of Friends Are You?(哈希)

What Kind of Friends Are You? Time Limit: 1 Second      Memory Limit: 65536 KB 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 Sands

zoj 3960(矩阵快速幂)

题意:有n个人坐成一排,每个人从1-m中选出一个数字,只有一个规则,如果相邻两个人选出的数字相同,这个数字必须大于等于k(k <= m),问n个人选数字一共有多少种方法. 题解:需要递推,定义一个数组f[i]表示要放第i个数字大于等于k的方法数,g[i]表示要放第i个数字小于k的方法,结果当然就是f[n] + g[n]. f[i] = f[i - 1] * (m - k) + g[i - 1] * (m - k) g[i] = f[i - 1] * k + g[i - 1] * (k - 1)

ZOJ 3812 We Need Medicine(牡丹江网络赛D题)

ZOJ 3812 We Need Medicine 题目链接 思路:dp[i][j][k]表示第i个物品,组成两个值为j和k的状态,这样会爆掉,所以状态需要转化一下 首先利用滚动数组,可以省去i这维,然后由于j最大记录到50,所以可以把状态表示成一个二进制数s,转化成dp[k] = s,表示组成k状态能组成s,这样空间复杂度就可以接受了,然后这题时限还可以,就这样去转移,然后记录下路径即可 代码: #include <cstdio> #include <cstring> #incl

ZOJ 3814 Sawtooth Puzzle(牡丹江网络赛F题)

ZOJ 3814 Sawtooth Puzzle 题目链接 记录状态广搜,把9个拼图都压缩成一个状态,然后去搜索,就是模拟的过程比较麻烦 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <set> using namespace std; typedef unsigned long long ll; int t; int

ZOJ 3879 Capture the Flag (浙江省省赛K题+模拟)

题目链接:ZOJ 3879 Capture the Flag 题意:给出n支队伍互相攻击服务器,有以下规则: 1.如果A队的服务器1被B队攻击成功,A队将失去n-1点分数,这n-1点分数会平分给成功攻击A队的服务器1的其他队伍. eg: 共4个队 A B 1 C B 1 此时A队和C队都获得1.5(3/2)分,B队失去3分 2.如果一个队不能维护好他们的服务器,该队将失去n-1分,这n-1分平分给其他能维护好服务器的队伍. eg: 1 1 0 1 0 1 1 1 针对服务器1(即第一行),只有C

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place

zoj 3822 Domination 概率dp 2014牡丹江站D题

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar