POJ2643 Election(MAP)

来源:互联网 发布:淘宝双11红包怎么用 编辑:程序博客网 时间:2024/05/18 16:39

题目大意:就是n对数据,上面一个是候选人姓名,下面一个是候选人党派,有个自由党也姑且当做一个特殊党派吧。然后m个数据是选票结果,求最后得胜者,得胜的要求是:其票数大于任何一个人。

思路:这道水题需要注意几个地方:The word "tie" if there is no winner; that is, if no candidate receives more votes than every other candidate.         不大于其余所有的人的票数就tie,即是说,赢得条件是要大于其余所有人的票数,即是说最大的数只能有一个。 还有一个要注意的是:Any names not in the list of candidates should be ignored.     也就是说还要下面票数的人如果是上面有过的名字才自加。但是这道题的数据很弱,第二个问题不处理居然也能过。


AC program:#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<math.h>#include<iomanip>#include<queue>#include<map>using namespace std;int main(){map<string ,string>mm;map<string ,int >gg;int n,m;string str1,str2; cin>>n;getchar();while(n--){  getline(cin,str1);  getline(cin,str2);  mm[str1]=str2;          }cin>>m;getchar();bool flag=false;int max=0,tmp; string tag;while(m--){  getline(cin,str1);  if(mm[str1].size()==0)continue;///note!!!! mm[str1]==""also ok  tmp=++gg[str1];  if(tmp>max){flag=true;max=tmp;tag=str1;}  else if(tmp==max){flag=false;}          }if(flag){cout<<mm[tag]<<endl;}else cout<<"tie"<<endl;system("pause");  return 0;}