Election
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3982 | Accepted: 1866 |
Description
Canada has a multi-party system of government. Each candidate is generally associated with a party, and the party whose candidates win the most ridings generally forms the government. Some candidates run as independents, meaning they are not associated with any party. Your job is to count the votes for a particular riding and to determine the party with which the winning candidate is associated.
Input
The first line of input contains a positive integer n satisfying 2 <= n <= 20, the number of candidates in the riding. n pairs of lines follow: the first line in each pair is the name of the candidate, up to 80 characters; the second line is the name of the party, up to 80 characters, or the word "independent" if the candidate has no party. No candidate name is repeated and no party name is repeated in the input. No lines contain leading or trailing blanks.
The next line contains a positive integer m <= 10000, and is followed by m lines each indicating the name of a candidate for which a ballot is cast. Any names not in the list of candidates should be ignored.
The next line contains a positive integer m <= 10000, and is followed by m lines each indicating the name of a candidate for which a ballot is cast. Any names not in the list of candidates should be ignored.
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
3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 6 John Smith Marilyn Manson Marilyn Manson Jane Doe John Smith Marilyn Manson
Sample Output
Rhinoceros
Source
一道简单的map题。主要是用来熟悉一下字符串的各种处理,巩固基础。
代码如下:
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<string,string> ss;//关联cand和party
map<string,int> st;//关联cand和num
int main()
{
int n,m,i;
char cand[85],party[85];
string result;//result 为string 类型
int num=0,maxn=-1;
bool flag;//flag用于判断
scanf("%d",&n);
getchar();//读取换行符
for(i=0;i<n;i++)
{
gets(cand);//gets()只能读取char类型的数据,所以cand和party不能是string类型
gets(party);
ss[cand]=party;//map支持char类型和string类型
}
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
gets(cand);
if(ss[cand]=="") continue;//如果没有出现过,继续
else
{
num=++st[cand];//统计得票最多的候选人名字,后面再转回政党,因为还有independent,不能将independent归于同一个政党
if(num==maxn)
flag=0;//先判断是否出现平局,如果出现平局,flag=0
else if(num>maxn)//不断更新最大值
{
result=cand;//result为string类型,可以用char类型的cand给它赋值,注意!!!若是两个char类型,不能这样直接赋值
maxn=num;
flag=1;//如果没有出现平局,flag=1
}
}
}
if(flag) cout<<ss[result]<<endl;//输出名字对应的政党,包括independent
else cout<<"tie\n";
//getchar();//最好注释掉,其实这道题有getchar()也可以AC
return 0;
}
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<string,string> ss;//关联cand和party
map<string,int> st;//关联cand和num
int main()
{
int n,m,i;
char cand[85],party[85];
string result;//result 为string 类型
int num=0,maxn=-1;
bool flag;//flag用于判断
scanf("%d",&n);
getchar();//读取换行符
for(i=0;i<n;i++)
{
gets(cand);//gets()只能读取char类型的数据,所以cand和party不能是string类型
gets(party);
ss[cand]=party;//map支持char类型和string类型
}
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
gets(cand);
if(ss[cand]=="") continue;//如果没有出现过,继续
else
{
num=++st[cand];//统计得票最多的候选人名字,后面再转回政党,因为还有independent,不能将independent归于同一个政党
if(num==maxn)
flag=0;//先判断是否出现平局,如果出现平局,flag=0
else if(num>maxn)//不断更新最大值
{
result=cand;//result为string类型,可以用char类型的cand给它赋值,注意!!!若是两个char类型,不能这样直接赋值
maxn=num;
flag=1;//如果没有出现平局,flag=1
}
}
}
if(flag) cout<<ss[result]<<endl;//输出名字对应的政党,包括independent
else cout<<"tie\n";
//getchar();//最好注释掉,其实这道题有getchar()也可以AC
return 0;
}