ZOJ 3960 What Kind of Friends Are You?

meaning of the title

A person has n friends, but now she only knows that n friends in a group can not tell who is who. She collects everyone's answers by asking some yes or no questions and compares them with her friend's answers. If the same person is her friend.

thinking

Firstly, each person's name is changed into an int by map hash, and then the map value is updated according to the person who answered yes to each question. Since there are only 20 questions at most, we can use binary to express the person's answer to each question. When comparing, we only need to compare two ints.
Code
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
string name[201];
map<string,int> mp;
int ans[201];
int main()
{
    int T,n,q,c,m,f,tt;
    string s;
    int temp,x;
    cin>>T;
    while(T--)
    {
        cin>>n>>q;
        cin>>c;
        for(int i=1;i<=c;i++)
        {
            cin>>name[i];
            mp[name[i]]=i;
        }
        for(int i=0;i<q;i++)
        {
            for(int j=1;j<=c;j++)
                ans[j]<<=1;
            cin>>m;
            for(int j=0;j<m;j++)
            {
                cin>>s;
                ans[mp[s]]++;
            }
        }
        for(int i=0;i<n;i++)
        {
            temp=0;
            f=0;
            for(int i=0;i<q;i++)
            {
                temp<<=1;
                cin>>x;
                temp+=x;
            }
            for(int i=1;i<=c;i++)
                if(ans[i]==temp)
                {
                    tt=i;
                    f++;
                }
            if(f!=1)
                printf("Let's go to the library!!\n");
            else cout<<name[tt]<<"\n";
        }
        for(int i=1;i<=c;i++)
            ans[i]=0;
        mp.clear();
    }
    return 0;
}

Posted by enterume on Thu, 14 Feb 2019 20:18:18 -0800