TOJ-1322 Election

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.

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: Waterloo
Local Contest Jun. 19, 1999

题目理解起来毫无难度,但是做的时候发生了很奇怪的事情。开始时,在输入n或m后,gets()前,我用cin.get或getchar吃多余字符,WA。。。

去掉cin.get或getchar(),TLE。。。为此我在网上把别人的代码抄了又抄,只有一个能AC,就是scanf("%d\n",&n)。为什么,我不知道,希望有大牛

能给我解释一下。改来改去我的代码和别人的也长的差不多了,本来我只用了一个map来着,不过两个map确实写起来容易多了。

#include <stdio.h>
#include <iostream>
#include <map>
#include <string.h>
#include <memory.h>
using namespace std;
struct str{
    char n[82];
    friend bool operator < (str a, str b)
    {
        return strcmp(a.n, b.n)<0;
    }
};

int main(){
    int i,n,m;
    str cant,pty;
    map<str,str> cdt;
    map<str,int> num;
    scanf("%d\n",&n);

    for(i=0;i<n;i++){
        gets(cant.n);
        gets(pty.n);
        cdt[cant] = pty;
        num[cant] = 0;
    }
    map<str,int>::iterator iter,temp;
    map<str,str>::iterator temp1;
    scanf("%d\n",&m);
    for(i=0;i<m;i++)
    {
        gets(cant.n);
        iter=num.find(cant);
        iter->second++;
    }
    int k = 0;
    temp=num.begin();
    for(iter=num.begin();iter!=num.end();iter++){
        if(iter->second == temp->second) k++;
        else
        {
            if(iter->second > temp->second)
            {
                temp = iter;
                k=1;
            }
        }
    }
    if(k>1)    printf("tie\n");
    else{
        temp1 = cdt.find(temp->first);
        printf("%s\n",temp1->second.n);
    }
    return 0;
}
时间: 2024-10-13 05:50:17

TOJ-1322 Election的相关文章

bzoj:1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区

Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which holds either a Holstein ('H') or Jersey ('J') cow. The Jerseys want to create a voting district of 7 contiguous (vertically or horizontally) cow l

POJ 3664 Election Time

Election Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7845   Accepted: 4176 Description The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 ≤ N ≤ 50,000) running

TOJ 2850 Corn Fields 状压dp

Source: http://acm.tju.edu.cn/toj/showp.php?pid=2850 题意:n*m的土地上种东西,每个位置分为可以种和不能,种的方案要求不能相邻地种,问合法方案数.(写得有点乱) 分析:做过炮兵阵地,这题就是秒杀了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int mo = 1000000

TOJ 2294 POJ 3286 How many 0&#39;s? 数位dp

http://acm.tju.edu.cn/toj/showp2294.html http://poj.org/problem?id=3284 题外话:集训结束,回学校了.在宿舍看了这题,没什么好想法,去洗澡了.转了两个澡堂都特么没开..倒是在路上把这题想了.用回自己的电脑,不得不说苹果的字体渲染,真心高一个等级. 题意:给定两个数a和b,从a写到b,问一共写了多少个0. 分析:当然先转化为求0..a写多少个0.网上有更简单的做法,就是枚举每位作为0,则如果这一位本来是0,左边取1..a-1(不

TOJ 4105 Lines Counting(离线树状数组)

4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Runs: 47 On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two interval

TOJ刷题记录(2/50)

P1172:二分,最大化最小值 1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <map> 9 #include <queue> 10

hihoCoder 1426 : What a Ridiculous Election(总统夶选)

hihoCoder #1426 : What a Ridiculous Election(总统夶选) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 In country Light Tower, a presidential election is going on. There are two candidates,  Mr. X1 and Mr. X2, and both of them are not like good per

TOJ 1139.Compromise

2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是字符串的比较. 原题链接:http://acm.tju.edu.cn/toj/showp.php?pid=1139 解题思路: 简单的最长公共子序列问题,只不过过程中比较的是两个字符串,故使用二维字符数组保存输入文本. 输入 x[1...m][], y[1...n][] ,c[i,j]代表两个文本的

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第一弹)

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 196  Solved: 116[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold

zookeeper 集群 Cannot open channel to X at election address Error contacting service. It is probably not running.

zookeeper集群   启动 1.问题现象. 启动每一个都提示  STARTED 但是查看 status时全部节点都报错 [[email protected] bin]# sh zkServer.sh  start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.8/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [[email protect