HDOJ.2094 产生冠军(map)

产生冠军

点我挑战题目
点我一起学习STL-MAP

题意分析

给出n组数据,代表a打败了b,让判断根据这n组数据是否能判断出来产生了冠军。一开始以为这道题很难,其实用map可以应付。
大原则,赢了的人置1,输了的人置0.
首先要知道胜利的人肯定不能输,①所以在map中输了的人置0,②并且如果这个人输过(也就是他的second已经为0了),即使是赢了的话,也不置为1。③最后遍历判断一下,map中是否只有1个一,如果是的话,就能产生冠军,否的话,就不能。

代码总览

/*
    Title:HDOJ.2094
    Author:pengwill
    Date:2016-11-21
*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <map>

using namespace std;
typedef map<string,int> mp;
mp p;
string s1,s2;
int main()
{
    cin.sync_with_stdio(false);
    //freopen("in.txt","r",stdin);
    int n,judge = 0,temp = 0;
    mp::iterator iter;
    while(cin>>n && n){
        judge = temp = 0;
        while(n--){
            cin.tie(0);
            cin>>s1>>s2;
            p[s2] = 0;
            //①
            if(p.count(s1) == 0){
                p[s1] = 1;
            }else{
                iter = p.find(s1);
                //②
                if(iter->second == 0){
                    continue;
                }else{
                    p[s1] = 1;
                }
            }

        }
        for(iter = p.begin();iter!= p.end();iter++){
                    //cout<<iter->first<<"	"<<iter->second<<endl;
                //③
                if(iter->second == 1){
                   judge++;
                }
        }
        if(judge == 1){
                cout<<"Yes"<<endl;
            }else{
                cout<<"No"<<endl;
            }
        p.clear();

    }
    return 0;
    //fclose(stdin);
}
原文地址:https://www.cnblogs.com/pengwill/p/7367225.html