标签:forms oca which opera ons str tle receives 题目
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.
3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 6 John Smith Marilyn Manson Marilyn Manson Jane Doe John Smith Marilyn Manson
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; }
标签:forms oca which opera ons str tle receives 题目
原文地址:http://www.cnblogs.com/shenchuguimo/p/6359240.html