poj2643 Election (map)

来源:互联网 发布:网络电视直播在线 编辑:程序博客网 时间:2024/06/02 06:01

题目:http://poj.org/problem?id=2643

Output

Output consists of a single line containing one of: 
  • The name of the party with whom the winning candidate is associated, if there is a winning candidate and that candidate is associated with a party. 
  • The word "independent" if there is a winning candidate and that candidate is not associated with a party. 
  • The word "tie" if there is no winner; that is, if no candidate receives more votes than every other candidate.

Sample Input

3Marilyn MansonRhinocerosJane DoeFamily CoalitionJohn Smithindependent6John SmithMarilyn MansonMarilyn MansonJane DoeJohn SmithMarilyn Manson

Sample Output

Rhinoceros

题意:

有n个候选人,每个人有两行数据,第一行为姓名 ,第二行为所属党派。

有m张选票,每张一行,代表所投的候选人的姓名,若所投的姓名不在上述名单中,应忽略。

若有唯一的人胜选,输出其所属党派,若多人同时获最高票,输出“tie”.

#include<stdio.h>#include<string.h>#include<map>#include<iostream>using namespace std;int main(){    int n,m;    char name[100];    char party[100];    scanf("%d",&n);    getchar();    map<string ,string > name1;    map<string ,int > name2;    while(n--)    {      gets(name);      gets(party);       name1[name]=party;    }    scanf("%d",&m);    getchar();    int s=0;    char ans[100];    int flag=1;    while(m--)    {        gets(name);        if(name1[name][0]!='\0')name2[name]++;  //判断被投票人的姓名是否在名单中        if(name2[name]>s)        {            s=name2[name];            strcpy(ans,name);            flag=1;        }        else            if(name2[name]==s)flag=0;    }    if(flag)printf("%s\n",name1[ans].c_str());    else printf("tie\n");    return 0;}