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

来源:互联网 发布:二维码生成软件设备 编辑:程序博客网 时间:2024/06/05 19:41

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?

给定 n 个待确定名字的 Friends 和 q 个问题。已知 c 个 Friends 的名字。

对于第 i 个问题,有 miFriends 会回答 yes ,其余 cmiFriends 均回答 no

现在给定 n 个待确定名字的 Friends 以及他们对于 q 个问题的回答。若能够确定它的名字,给出;否则,输出 Let's go to the library!!

解题思路

将问题数值化,第 i 个问题回答 yes 所代表的数值为 1<<i 。则 c 个 Friends 都有唯一确定的一个数值与之对应。相应的,若一个数值能够唯一确定一个 Friends ,则对于下列 n 个待确定名字的 Friends ,同样将回答的 yes 数值化,则可确定该 Friends 的名字;否则输出 Let's go to the library!!

代码

#include<bits/stdc++.h>using namespace std;int n, q, c, m;string name;map<string, int> mp;map<int, string> rev;int main(){    int T;    scanf("%d",&T);    while(T--)    {        mp.clear();        rev.clear();        scanf("%d %d",&n,&q);        scanf("%d",&c);        for(int i=1;i<=c;i++)            cin>>name,  mp[name] = 0;        for(int i=0;i<q;i++)        {            scanf("%d", &m);            for(int j=0;j<m;j++)                cin>>name,                mp[name] += (1<<i);        }        for(map<string, int>::iterator it=mp.begin();it!=mp.end();it++)        {            if( rev.find(it->second) == rev.end() )                rev[it->second] = it->first;            else                rev[it->second] = "Let's go to the library!!";        }        for(int i=0;i<n;i++)        {            int val = 0;            for(int j=0, a;j<q;j++)                scanf("%d",&a), val += (1<<j)*a;            if(rev.find(val) == rev.end())                printf("Let's go to the library!!\n");            else    printf("%s\n", rev[val].c_str());        }    }}
0 0
原创粉丝点击