Re: 10374 - Election
Posted: Wed Apr 24, 2013 11:46 am
My code now got AC. I changed the cin with gets and scanf. I don't understand how it became AC. Can you explain it?
Code: Select all
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define pf printf
#define sf scanf
#define maxm 10000
#define eps 1e-10
#define mem(a,b) memset(a,b,sizeof(a))
char st_can[30][100],st_party[30][100],str[100];
int res[50],a,b;
void search(){
int i,j,k;
for(i=0;i<a;i++){
if(strcmp(str,st_can[i])==0){
res[i]++;
return;
}
}
}
int main(){
int i,j,k,n,t,kase=0; char ch;
sf("%d",&t);
for(k=1;k<=t;k++){
getchar();
sf("%d",&a);
getchar();
for(i=0;i<a;i++){
gets(st_can[i]);
gets(st_party[i]);
}
sf("%d",&b);
getchar();
for(i=0;i<b;i++){
gets(str);
search();
}
int tag=0,max=-1,win;
for(i=0;i<a;i++){
if(res[i]>max){
max=res[i];
win=i;
tag=0;
}
else if(res[i]==max){
tag=1;
max=-1;
}
}
if(tag==1) pf("tie\n");
else{
pf("%s\n",st_party[win]);
}
mem(st_party,0);
mem(st_can,0);
mem(res,0);
if(k!=t) pf("\n");
}
return 0;
}
Code: Select all
4
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson
Code: Select all
Rhinoceros
Rhinoceros
Rhinoceros
Rhinoceros
Code: Select all
1
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Jane Doe
Code: Select all
tie
Code: Select all
1
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
1
asdf
Probably there is something weird about the judge input. I took all inputs with gets,removed trailing and leading spaces,and finally got ac. ""No lines contain leading or trailing blanks. "" is not correct.
While there may be leading / trailing spaces in the lines that contain numbers (which, I agree, is not what the problem states), there doesn't seem to be anything weird or atypical in regards to spaces on the balance of the lines (the ones that contain the names). In other words, the following outline code, worked perfectly in reading the inputF*** this stupid problem. Not only the input contains leading and trailing spaces, there can be "futile" blank lines throughout the judge data which must be ignored in order to get Accepted. The judge data is SHIT...I request the admins to fix this. This kind of problem teaches you nothing but only piss you off/
Code: Select all
#include<bits/stdc++.h>
using namespace std;
int main() {
/* Variables used in the program */
int candidates, i, j, names, testCases;
string candidate, party;
/* Read in the number of test cases */
scanf("%d\n", &testCases);
/* Process the test cases */
for(i = 0; i < testCases; i++) {
/* Print a newline between cases */
if(i != 0) {
printf("\n");
}
/* Read in the number of candidates */
scanf("%d\n", &candidates);
/* Process the candidates */
for(j = 0; j < candidates; j++) {
/* Read in the candidate and the party they belong to */
getline(cin, candidate); getline(cin, party);
// Do stuff
}
/* Read in the number of names */
scanf("%d\n", &names);
/* Process the names */
for(j = 0; j < names; j++) {
/* Get the candidate name */
getline(cin, candidate);
// Do stuff
}
}
return 0;
}
What's being said here is thatNo candidate name is repeated and no party name is repeated in the input.
Code: Select all
Sara Shahida
Jaguar
Sara Shahida
Jaguar
Code: Select all
Sara Shahida
Jaguar
Jacob Holger
Jaguar
Code: Select all
2
2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
3
Jacob Holger
Sara Shahida
Jacob Holger
2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
2
Jacob Holger
Sara Shahida
Code: Select all
Jaguar
tie
You know, the very least you could do before you ask for help is to run your code on the test cases provided on the thread. I mean they are there for a reason.mamun4122 wrote:why runtime error![]()
There ARE leading and trailing spaces in "number"(integer n and m.. see the problem description) inputs
There ARE extra blank lines between "CONSECUTIVE" cases. It means that before the input of "integer n" there can be one or more than one blanks in the input.
If you handle these and if your logics are correct, then you will surely get Accepted. If you have other opinions, then please do share.There are no extra blanks before or after the "integer m"
Code: Select all
#include<bits/stdc++.h>
#define FRU freopen("out.txt","w",stdout)
#define FRO freopen("in.txt","r",stdin)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(ara,n) memset(ara,n,sizeof ara)
#define loop(i,j,n) for(i=j;i<n;i++)
#define rloop(i,j,n) for(i=n;i>=j;i--)
#define INF 2147483647
//const int row[]={-1, -1, -1, 0, 0, 1, 1, 1}; // Kings Move
//const int col[]={-1, 0, 1, -1, 1, -1, 0, 1}; // Kings Move
//const int row[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int col[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
//const int row[]={-1,0,0,1,0};
//const int col[]={0,-1,1,0,0};
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
return ((a*b)/gcd(a,b));
}
using namespace std;
int main()
{
//FRO;
//FRU;
//std::ios_base::sync_with_stdio(false);
int a,b,c,i,j,k,tc,t=0;
int n,m,cnt=0;
map<string,string>mmp1;
map<string,int>mmp2;
string s,s1;
vector<string>vec;
scanf("%d",&tc);
//getchar();
while(tc--)
{
mmp1.clear();
mmp2.clear();
vec.clear();
pair<int,string>pp[1001];
s.clear();
s1.clear();
if(t)printf("\n");
t++;
scanf("%d",&n);
//getchar();
for(i=0; i<n; i++)
{
getline(cin,s);
if(s[0]=='\0'|| s[0]==' ')
{
i--;
continue;
}
getline(cin,s1);
mmp1[s]=s1;
}
scanf("%d",&m);
i=m;
//getchar();
while(i--)
{
getline(cin,s);
if(s[0]=='\0'|| s[0]==' ')
{
//cout<<s<<endl;
i++;
continue;
}
if(mmp1.find(s)!=mmp1.end())
{
if(mmp2.find(s)==mmp2.end())vec.pb(s);
mmp2[s]++;
}
}
for(i=0; i<vec.size(); i++)pp[i]=mp(mmp2[vec[i]],vec[i]);
int len=vec.size();
sort(pp,pp+len);
//printf("%d %d\n",pp[len-1].ff,pp[len-2].ff);
if(len==1|| (m>1&& pp[len-1].ff!= pp[len-2].ff))cout<<mmp1[pp[len-1].ss]<<endl;
else printf("tie\n");
}
return 0;
}
Code: Select all
#include <iostream>
using namespace std;
int main() {
int ncases, np, nv, N = 0;
string line, candidate, party;
cin >> ncases;
while (ncases--) {
cin >> np; getline(cin , line); /* drain newline after reading int */
for (int i = 0; i < np; i++) {
getline(cin , candidate); getline(cin , party);
//process candidate and party
}
cin >> nv; getline(cin , line); /* drain newline after reading int */
for (int i = 0; i < nv; i++) {
getline(cin , candidate);
//process candidate
}
//process data
//string theWinningCandidate = ...
//bool thereIsATie = ...
if (N++)
cout << endl;
if (thereIsATie)
cout << "tie" << endl;
else
cout << theWinningCandidate << endl;
}
return 0;
}
Code: Select all
9
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson
3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
Brian Fry
Brian Fry
Brian Fry
Brian Fry
Brian Fry
Brian Fry
3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
6
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
2
Jan
Jaguar
Brian Fry
Jaguar
2
Brian Fry
Jan
2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
1
Brian Fry
3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
John Smith
3
stanley toles
Rhinoceros
jan guru
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
John Smith
3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
Brian Fry
3
Brian Fry
Rhinoceros
Sohel
Rhinoceros
John Smith
Rhinoceros
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
Sohel
Code: Select all
Rhinoceros
tie
tie
tie
tie
tie
independent
Rhinoceros
Rhinoceros