• 浙江省赛C What Kind of Friends Are You?(MAP)


    题目http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3960

    题意:对01矩阵中第i行j个数 1表示第j个问题中的名字和c个已知名字取交集 

        0表示已知名字去除第j个问题中的名字

        问对于矩阵中i行 是否存在q个问题中的名字和c个已知名字的元素个数为1的交集

        若有输该名字 否则输出Let's go to the library!!

    路:将第i个问题中的Yes数值化为1<<i(压位 相当于转化为二进制数后左移i位) 这样可以保证c中名字有唯一对应值 

        要让名字和值对应需要用到 STL中的map<string,int>

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        string s,s0;
        int t,n,q,c,m,a;
        map<string,int> mp;
        map<string,int>::iterator it;
    
        cin>>t;
        while(t--){
            mp.clear();
            cin>>n>>q;
            cin>>c;
            for(int i=0;i<c;i++){
                cin>>s0;
                mp[s0]=0;//需初始化 否则会WA
            }
            for(int i=0;i<q;i++){
                cin>>m;
                for(int j=0;j<m;j++){
                    cin>>s;
                    mp[s]+=1<<i;
                }
            }
            for(int i=0;i<n;i++){
                int sum=0;
                for(int j=0;j<q;j++){
                    cin>>a;
                    if(a==1)
                        sum+=1<<j;
                }
                int cnt=0;
                string name;
    
                for(it=mp.begin();it!=mp.end();it++){//遍历图 是否存在唯一对应值
                    if(it->second==sum){
                        name=it->first;
                        cnt++;
                    }
                }
                if(cnt==1)
                    cout<<name<<endl;
                else
                    cout<<"Let's go to the library!!"<<endl;
            }
        }
        return 0;
    }
    View Code

        

        

  • 相关阅读:
    python之道04
    python之list [ 列表 ]
    end和sep的使用方法
    pass1
    python之for (循环)
    python之range (范围)
    python之str (字符型)
    python之bool (布尔值)
    python之int (整型)
    python之道03
  • 原文地址:https://www.cnblogs.com/NDKY9/p/6771220.html
Copyright © 2020-2023  润新知