10194 - Football (aka Soccer)

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
animenologist
New poster
Posts: 7
Joined: Tue Jan 22, 2008 5:55 am

Post by animenologist »

Here's the input and output I used. I used it to test whether or not the comparisons were working correctly, by creating two teams that would make it all the way to that level. Also, my last case, I had them with different cases to make sure that they were sorted by lexicographical order.

Code: Select all

INPUT

6
Test 1
4
A
B
C
D
3
A#3@1#C
A#1@1#C
B#3@0#D
TEST 2
4
A
B
C
D
5
A#3@1#C
B#1@1#D
B#1@1#D
B#1@1#D
B#0@1#D
TEST 3
4
A
B
C
D
2
A#2@1#C
B#3@1#D
TEST 4
4
A
B
C
D
3
A#3@1#C
B#4@1#D
B#1@2#D
TEST 5
4
A
B
C
D
3
B#3@1#C
A#3@0#D
A#0@1#D
TEST 6
5
aa
BB
c
D
eE
0
Test 6
4
A
B
C
D
3
A#1@3#C
A#1@1#C
B#0@3#D

Code: Select all


OUTPUT

Test 1
1) A 4p, 2g (1-1-0), 2gd (4-2)
2) B 3p, 1g (1-0-0), 3gd (3-0)
3) C 1p, 2g (0-1-1), -2gd (2-4)
4) D 0p, 1g (0-0-1), -3gd (0-3)

TEST 2
1) D 6p, 4g (1-3-0), 1gd (4-3)
2) A 3p, 1g (1-0-0), 2gd (3-1)
3) B 3p, 4g (0-3-1), -1gd (3-4)
4) C 0p, 1g (0-0-1), -2gd (1-3)

TEST 3
1) B 3p, 1g (1-0-0), 2gd (3-1)
2) A 3p, 1g (1-0-0), 1gd (2-1)
3) C 0p, 1g (0-0-1), -1gd (1-2)
4) D 0p, 1g (0-0-1), -2gd (1-3)

TEST 4
1) B 3p, 2g (1-0-1), 2gd (5-3)
2) A 3p, 1g (1-0-0), 2gd (3-1)
3) D 3p, 2g (1-0-1), -2gd (3-5)
4) C 0p, 1g (0-0-1), -2gd (1-3)

TEST 5
1) B 3p, 1g (1-0-0), 2gd (3-1)
2) A 3p, 2g (1-0-1), 2gd (3-1)
3) D 3p, 2g (1-0-1), -2gd (1-3)
4) C 0p, 1g (0-0-1), -2gd (1-3)

TEST 6
1) aa 0p, 0g (0-0-0), 0gd (0-0)
2) BB 0p, 0g (0-0-0), 0gd (0-0)
3) c 0p, 0g (0-0-0), 0gd (0-0)
4) D 0p, 0g (0-0-0), 0gd (0-0)
5) eE 0p, 0g (0-0-0), 0gd (0-0)

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho »

Here's a few suggestions:

(1) I am not sure why you're reading from football.in? I used C++ and it should read from standard in. May be it's different for java? Or are you just been training too much? :P

(2) Don't trim the team names, as spaces are allowed, "a--" and "a---" are actually different teams, where "-" represents a space. For the same reason, don't trim a game line. But my guess is this is not the reason it is failing.

(3) The case insensitive sort can be a problem too. Sure, using java's case insensitive compare looks perfect, but in the old days of 10194 (that's like 5-6 years ago), people like to change everything to upper case when comparing, so it will mis-compare '[' (ASCII 91) with 'a' (ASCII 97), since the program changed the 'a' to 'A' (ASCII 65). Of course, just changing everything to uppercase is really an incorrect approach and I suspect java is using the correct approach. In other words, the judging solution may be using the theoretically wrong approach. This is something you can try messing with.
animenologist
New poster
Posts: 7
Joined: Tue Jan 22, 2008 5:55 am

Post by animenologist »

The football.in thing is something I do when I program normally, having it read in from a default file. I use it to keep records, so that I have a record of all my input and give them proper names to go with the program. When I submit, I switch the files to System.in for standard input.

Anyways, this is my newest code (with the normal standard input this time)

Code: Select all


import java.io.*;
import java.util.*;

public class Main implements Comparable<Main>{
	
	public String name, reducedName;
	public int points, wins, losses, ties, scored, against, played;
	
	public Main(String x){
		this.name = x;
		this.points = 0;
		this.wins = 0;
		this.losses = 0;
		this.ties = 0;
		this.scored = 0;
		this.against = 0;
		this.played = 0;
	}
	
	public int compareTo(Main x){
		
		if(this.points != x.points)
			return x.points-this.points;		
		if(this.wins != x.wins)
			return x.wins-this.wins;		
		if((this.scored-this.against) != (x.scored-x.against))
			return ((x.scored-x.against) - (this.scored-this.against) );		
		if(this.scored != x.scored)
			return x.scored-this.scored;		
		if(this.played != x.played)
			return this.played-x.played;		
		return this.stringCompare(x.name);

	}
	
	public int stringCompare(String x){
		
		for(int a = 0;a < Math.min(this.name.length(), x.length());a++){
			if(Character.isLetter(this.name.charAt(a)) && Character.isLetter(x.charAt(a))){
				if(Character.toUpperCase(this.name.charAt(a)) != Character.toUpperCase(x.charAt(a)))
					return Character.toUpperCase(this.name.charAt(a))-Character.toUpperCase(x.charAt(a));
			}
			else{
				if(this.name.charAt(a) != x.charAt(a))
					return this.name.charAt(a)-x.charAt(a);
			}
		}
		
		return this.name.length()-x.length();
	}
	
	public static void main(String[] args)throws Exception{
		
		Scanner In = new Scanner(System.in);
		TreeMap<String, Integer> mapping; 
		int numGames = Integer.parseInt(In.nextLine());
		
		for(int currGame = 0;currGame < numGames;currGame++){
			String tournament = In.nextLine();
			int numTeams = Integer.parseInt(In.nextLine());
			mapping = new TreeMap<String, Integer>();
			Main teams[] = new Main[numTeams];
			for(int currTeam = 0;currTeam < numTeams;currTeam++){
				String teamName = In.nextLine();
				teams[currTeam] = new Main(teamName);
				mapping.put(teamName, new Integer(currTeam));
			}
			int numMatches = Integer.parseInt(In.nextLine());
			for(int currMatch = 0;currMatch < numMatches;currMatch++){
				String game = In.nextLine();
				String teamOne = game.substring(0, game.indexOf("#"));
				game = game.substring(game.indexOf("#")+1);
				int scoreOne = Integer.parseInt(game.substring(0, game.indexOf("@")));
				game = game.substring(game.indexOf("@")+1);
				int scoreTwo = Integer.parseInt(game.substring(0, game.indexOf("#")));
				game = game.substring(game.indexOf("#")+1);
				String teamTwo = game;
				teams[mapping.get(teamOne).intValue()].scored+= scoreOne;
				teams[mapping.get(teamOne).intValue()].against+= scoreTwo;
				teams[mapping.get(teamOne).intValue()].played++;
				teams[mapping.get(teamTwo).intValue()].scored+= scoreTwo;
				teams[mapping.get(teamTwo).intValue()].against+= scoreOne;
				teams[mapping.get(teamTwo).intValue()].played++;
				if(scoreOne > scoreTwo){
					teams[mapping.get(teamOne).intValue()].wins++;
					teams[mapping.get(teamOne).intValue()].points+= 3;
					teams[mapping.get(teamTwo).intValue()].losses++;
				}
				else if(scoreTwo > scoreOne){
					teams[mapping.get(teamTwo).intValue()].wins++;
					teams[mapping.get(teamTwo).intValue()].points+= 3;
					teams[mapping.get(teamOne).intValue()].losses++;					
				}
				else{
					teams[mapping.get(teamOne).intValue()].ties++;
					teams[mapping.get(teamOne).intValue()].points++;
					teams[mapping.get(teamTwo).intValue()].ties++;
					teams[mapping.get(teamTwo).intValue()].points++;
				}
			}
			Arrays.sort(teams);
			if(currGame!=0)
				System.out.println();
			System.out.println(tournament);
			for(int currTeam = 0;currTeam < numTeams;currTeam++){
				System.out.println((currTeam+1)+") "+teams[currTeam].name+" "+teams[currTeam].points+"p, "+		
				teams[currTeam].played+"g ("+teams[currTeam].wins+"-"+teams[currTeam].ties+"-"+teams[currTeam].losses+"), "+
				(teams[currTeam].scored-teams[currTeam].against)+"gd ("+teams[currTeam].scored+"-"+teams[currTeam].against+")");
			}
		}
	}
	
}

If my problem was truly the difference in cases and the characters between the two cases, I created this new function

Code: Select all


	public int stringCompare(String x){
		
		for(int a = 0;a < Math.min(this.name.length(), x.length());a++){
			if(Character.isLetter(this.name.charAt(a)) && Character.isLetter(x.charAt(a))){
				if(Character.toUpperCase(this.name.charAt(a)) != Character.toUpperCase(x.charAt(a)))
					return Character.toUpperCase(this.name.charAt(a))-Character.toUpperCase(x.charAt(a));
			}
			else{
				if(this.name.charAt(a) != x.charAt(a))
					return this.name.charAt(a)-x.charAt(a);
			}
		}
		
		return this.name.length()-x.length();
	}
It goes through each character and checks if they're both letters. If so, changes them both to uppercase and compares. If not, compares them as normal. If they are lexicographically equal up to the length of the shortest string, it gives precedence for the shorter of the 2. So what this means is that a > [ > A == a. But still, a WA. Can't think of what else could be wrong.
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho »

What is wrong is that you're actually doing it the right way. Do your case insensitive compare as follows: change all uppercase to lowercase then use regular string compare.
animenologist
New poster
Posts: 7
Joined: Tue Jan 22, 2008 5:55 am

Post by animenologist »

Tried everything. Tried toLowercase comparison, toUpperCase comparison, compareToIgnoreCase, and my own function. Tried trimming, not trimming, removing leading whitespace. Nothing seems to be working. Is there any logical errors with my comparisons? I think I'm following the rules correctly.
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho »

Everything looks right......may be try to do it in C++? I know in the old days, if java gives a RE sometimes it's reported as WA, as the judge would read the error messages as the output of your program. But I am not sure if that's still true after the upgrade...probably not.
tiagowanderley
New poster
Posts: 4
Joined: Thu May 01, 2008 3:49 pm

Re: 10194 - Football (aka Soccer) Why WA?

Post by tiagowanderley »

Hi,
my program is calculating everything correct. I'm receiving AW, I'm almost sure que is because of the lexicographical order. My program is written in java. Below it is the code .

Code: Select all

	import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int qntCamp = in.nextInt();
		in.nextLine();
		HashMap<String,Team> times; 
		String[] team1, team2;
		for (int i = 0; i < qntCamp; i++) {
			Object teams[];
			times = new HashMap<String,Team>();
			String nomeCamp = in.nextLine();
			int qntTimes = in.nextInt();
			in.nextLine();
			for (int j = 0; j < qntTimes; j++) {
				String time = in.nextLine();
				times.put(time,new Team(time));
			}
			int qntJogos = in.nextInt();
			in.nextLine();
			for (int j = 0; j < qntJogos; j++) {
				String jogo = in.nextLine();
				String placar[] = jogo.split("@");				
				team1 = placar[0].split("#");				
				team2 = placar[1].split("#");
				atualizaDados(team1[0],Integer.parseInt(team1[1]),times.get(team1[0]),team2[1],Integer.parseInt(team2[0]),times.get(team2[1]),times);
			}
			teams = times.values().toArray();
			Arrays.sort(teams);	
			System.out.println(nomeCamp);
			for (int k = 0; k < teams.length; k++) {
				Team t = (Team)teams[k];
				int n = k+1;
				System.out.println(t.toString(n));
			}	
			System.out.println();
		}
	}

	public static void atualizaDados(String time1, int golsT1, Team t1,
			String time2, int golsT2, Team t2,HashMap<String,Team> times) {
		times.get(time1).setGolsContra(golsT2);
		times.get(time1).setGolspro(golsT1);
		times.get(time1).setSaldoGols();
		times.get(time1).setQntJogos();
		times.get(time2).setGolsContra(golsT1);
		times.get(time2).setGolspro(golsT2);
		times.get(time2).setSaldoGols();
		times.get(time2).setQntJogos();
		if(golsT1 > golsT2){
			times.get(time1).setPontos(3);
			times.get(time1).setVitorias();
			times.get(time2).setDerrotas();
		}
		else if(golsT2 > golsT1){
			times.get(time2).setPontos(3);
			times.get(time2).setVitorias();
			times.get(time1).setDerrotas();
		}
		else{
			times.get(time1).setPontos(1);
			times.get(time2).setPontos(1);
			times.get(time1).setEmpate();
			times.get(time2).setEmpate();
		}

	}
}

class Team implements Comparable<Object>{
	int pontos, vitorias, golspro, golsContra, saldoGols, qntJogos,empate,derrotas;
	String nome;
	public Team(String nome){
		this.nome = nome;
		this.vitorias = 0;
		this.golspro = 0;
		this.golsContra = 0;
		this.saldoGols = 0;
		this.qntJogos = 0;
		this.empate = 0;
		this.derrotas = 0;

	}

	public int getPontos() {
		return pontos;
	}

	public void setPontos(int pontos) {
		this.pontos += pontos;
	}

	public int getVitorias() {
		return vitorias;
	}

	public void setVitorias() {
		this.vitorias++;
	}

	public int getGolspro() {
		return golspro;
	}

	public void setGolspro(int golspro) {
		this.golspro += golspro;
	}

	public int getGolsContra() {
		return golsContra;
	}

	public void setGolsContra(int golsContra) {
		this.golsContra += golsContra;
	}

	public int getSaldoGols() {
		return saldoGols;
	}

	public void setSaldoGols() {
		this.saldoGols = this.golspro - this.golsContra;
	}

	public int getQntJogos() {
		return qntJogos;
	}

	public void setQntJogos() {
		this.qntJogos++;;
	}

	public int compareTo(Object o) {
		Team t = (Team) o;
		if(t.getPontos() > pontos)
			return 1;
		else if(t.getPontos() < pontos) 
			return -1;
		else if(t.getVitorias() > vitorias)
			return 1;
		else if(t.getVitorias() < vitorias)
			return -1;
		else if(t.getSaldoGols() > saldoGols)
			return 1;
		else if(t.getSaldoGols() < saldoGols)
			return -1;
		else if(t.getGolspro() > golspro)
			return 1;
		else if(t.getGolspro() < golspro)
			return -1;
		else if(t.getQntJogos() > qntJogos)
			return -1;
		else if(t.getQntJogos() < qntJogos)
			return 1;
		else{
			int min = Math.min(t.getNome().length(),nome.length());
			String tUP = t.getNome().toUpperCase();
			String nUP = nome.toUpperCase();
			int retorno = 0;
			for (int i = 0; i < min; i++) {
				if(tUP.charAt(i) == nUP.charAt(i)){
						if(t.getNome().charAt(i) > nome.charAt(i))
							retorno = -1;
						else if(t.getNome().charAt(i) < nome.charAt(i))
							retorno = 1;	
				}
				else{
					if(tUP.charAt(i) > nUP.charAt(i))
						return -1;
					else if(tUP.charAt(i) < nUP.charAt(i))
						return 1;
				}
				if(t.getNome().length() > nome.length() && retorno == 0)
					return 1;
				else if (t.getNome().length() < nome.length() && retorno == 0)
				    return -1;
				
			}
			return retorno;
		}
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String toString(int num){
		return (num + ") "+this.nome + " " + this.pontos+"p,"+" "+qntJogos+"g"+" ("+vitorias+"-"+empate+"-"+derrotas+"), "+saldoGols+"gd ("+golspro+"-"+golsContra+")");
	}

	public int getEmpate() {
		return empate;
	}

	public void setEmpate() {
		this.empate++;
	}

	public int getDerrotas() {
		return derrotas;
	}

	public void setDerrotas() {
		this.derrotas++;
	}
}
Help-me please.
stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Re:

Post by stcheung »

AliSaleh wrote:Hello
I have tested may code against a lot of test cases but it still WA
I don't know what to do If any one can help me this is my code.
sorry it's long .

Code: Select all

#include <iostream> 
#include <string> 
#include <map> 
#include <algorithm> 
#include <cstdlib> 
#include <vector> 
using namespace std; 
struct team{ 
	int play; int win; 
	int loss; int tie; 
	int gin; int gout; 
	int point; string name; 
	string realName; 
}; 
struct class1{ 
public : 
	bool operator()(team t1,team t2){ 
		if(t1.point!=t2.point) 
			return t1.point>t2.point; 
		else if(t1.win!=t2.win) 
			return t1.win>t2.win; 
		else if((t1.gout-t1.gin)!=(t2.gout-t2.gin)) 
			return (t1.gout-t1.gin)>(t2.gout-t2.gin); 
		else if(t1.gout!=t2.gout) 
			return t1.gout>t2.gout; 
		else if(t1.play!=t2.play) 
			return t1.play<t2.play; 
		else 
			return lexicographical_compare(t1.name.begin(),t1.name.end(),t2.name.begin(),t2.name.end()); 
	} 
}comparison; 

map<string,team> mp; 

int main() 
{ 
	string str,all,team1,team2,goal="",title1,name; 
	team temp; 
	int N,T,G; 
	int first,second1,g1,g2; 
	vector<team> temp2; 
	map<string,team>::iterator team_1; 
	map<string,team>::iterator team_2; 
	
	temp.gin=temp.gout=temp.loss=temp.win=temp.tie=temp.play=temp.point=0; 
	temp.name=""; 
	cin>>N; 
	for(N;N>0;N--){ 
		cin.ignore(); 
		char title[101]; 
		cin.getline(title,101); 
		title1=title; 
		cin>>T; 
		for(T;T>0;T--){ 
			cin.ignore(); 
			char ch[31]; 
			cin.get(ch,31); 
			name=str=ch; 
			for(int i=0;i<(int)team1.size();i++) 
				name[i]=tolower(str[i]); 
			temp.name=name; 
			mp.insert(make_pair(str,temp)); 
		} 
		cin>>G; 
		for(G;G>0;G--){ 
			cin.ignore(); 
			char ch[70]; 
			cin.get(ch,70); 
			all=ch; 
			first=(int)all.find("#"); 
			second1=(int)all.rfind("#"); 
			team1.insert(0,all,0,first); 
			team2.insert(0,all,second1+1,all.size()-1+second1); 
			first++; 
			while(all[first]!='@'){ 
				goal+=all[first]; 
				first++; 
			} 
			g1=atoi(goal.c_str()); 
			goal=""; 
			first++; 
			while(first!=second1){ 
				goal+=all[first]; 
				first++; 
			} 
			g2=atoi(goal.c_str()); 
			goal=""; 
			
			team_1=mp.find(team1); 
			team_2=mp.find(team2); 
			if(g1>g2){ 
				team_1->second.win++; 
				team_2->second.loss++;} 
			else if(g2>g1){ 
				team_1->second.loss++; 
				team_2->second.win++;} 
			else{ 
				team_1->second.tie++; 
				team_2->second.tie++; 
			} 
			
			team_1->second.play++; 
			team_1->second.gout+=g1; 
			team_1->second.gin+=g2; 
			team_1->second.point=team_1->second.win*3+team_1->second.tie*1; 
			team_1->second.realName=team1; 
			
			team_2->second.play++; 
			team_2->second.gout+=g2; 
			team_2->second.gin+=g1; 
			team_2->second.point=team_2->second.win*3+team_2->second.tie*1; 
			team_2->second.realName=team2; 
			
			team1=""; 
			team2=""; 
			
		} 
		team_1=mp.begin(); 
		while(team_1!=mp.end()){ 
			temp2.push_back(team_1->second); 
			team_1++; 
		} 
		sort(temp2.begin(),temp2.end(),comparison); 
		cout<<title1<<endl; 
		for(int x=0;x<(int)temp2.size();x++){ 
			cout<<x+1<<") "<<temp2[x].realName<<" "<<temp2[x].point<<"p, "; 
			cout<<temp2[x].play<<"g "<<"("<<temp2[x].win<<"-"<<temp2[x].tie; 
			cout<<"-"<<temp2[x].loss<<"), "<<(temp2[x].gout-temp2[x].gin)<<"gd ("; 
			cout<<temp2[x].gout<<"-"<<temp2[x].gin<<")"; 
			if(x!=(int)temp2.size()-1) 
				cout<<endl; 
		} 
		if(N!=1) 
			cout<<endl<<endl; 
		title1=""; 
		mp.clear(); 
		temp2.clear(); 
	} 
	return 0; 
}
EDIT by moderator : Use code tags.
Thanks for your program. I had no luck getting my Java program to be accepted even though it passes all the test cases here and that I read every single thread couple times already. I also double checked my code 10 times and tried all the different variations of "case-insensitive" comparisons (convert to lowercase first, convert to uppercase first, only convert when both characters are letters). Still no luck so I suspect it's some Java-specific issue. Did ANYONE get accepted with a Java solution?

So I decided to do it in C++ but my C++ is rusty so I took your program and got accepted after fixing the bugs in it. There are couple bugs, all pretty obvious in my opinion:
(1) You missed a new line at the end
(2) You set the realName variable ONLY when the team is referenced in the list of games. This is unnecessary. Simply set the realName variable when you are reading the list of team names.
(3) You are doing a case-sensitive comparison. Instead, you should first convert both strings to uppercase and then call the lexicographical_compare() on them. In fact, converting both to lowercase would also work.

Normally I would consider it unethical to get AC by tweaking someone else's program. But in this case I feel like I know the problem in-and-out so I don't feel so guilty about it. I just suspect if anyone can really get accepted in Java!!!
stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Re: 10194 - Football (aka Soccer)

Post by stcheung »

Here's my code in Java. It looks long but half of it is just helper methods. Actual code is <120 lines. Java is such an expressive language (esp 1.6)

Code: Select all

import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;

class Main {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));	
	static int[] intBuffer = new int[0];
	static int intBufferPos = 0;
	static double[] doubleBuffer = new double[0];
	static int doubleBufferPos = 0;
	static StringBuilder outputBuffer = new StringBuilder(10000);
	static Scanner scanner = new Scanner(new BufferedInputStream(System.in));
	
	static int[] readIntArr() throws Exception {
		while(true) {
			String line = readLine();
			if(line == null) 
				return null;
			else if(line.equals(""))
				continue;
			else return toIntArr(line);
		}
	}
	
	static int[] toIntArr(String str) {
		if(str.equals(""))
			return new int[0];
		
		String strArr[] = toStrArr(str);
		int intArr[] = new int[strArr.length];
		for(int i=0; i<strArr.length; i++)
			intArr[i] = toInt(strArr[i]);
		
		return intArr;
	}
	
	static int readInt() throws Exception {
		while(intBufferPos >= intBuffer.length) {
			intBuffer = readIntArr();
			intBufferPos = 0;
		}
		return intBuffer[intBufferPos++];
	}
	
	static int toInt(String str) {
		return Integer.parseInt(str);
	}
	
	static long toLong(String str) {
		return Long.parseLong(str);
	}
	
	static double[] readDoubleArr() throws Exception {
		while(true) {
			String line = readLine();
			if(line == null) 
				return null;
			else if(line.equals(""))
				continue;
			else return toDoubleArr(line);
		}
	}
	
	static double[] toDoubleArr(String str) {
		if(str.equals(""))
			return new double[0];
		
		String strArr[] = toStrArr(str);
		double doubleArr[] = new double[strArr.length];
		for(int i=0; i<strArr.length; i++)
			doubleArr[i] = toDouble(strArr[i]);
		
		return doubleArr;
	}
	
	static double readDouble() throws Exception {
		while(doubleBufferPos >= doubleBuffer.length) {
			doubleBuffer = readDoubleArr();
			doubleBufferPos = 0;
		}
		return doubleBuffer[doubleBufferPos++];
	}
	
	static double toDouble(String str) {
		return Double.parseDouble(str);
	}
	
	static String[] toStrArr(String str) {
		StringTokenizer st = new StringTokenizer(str);
		String strArr[] = new String[st.countTokens()];
		int i=0;
		while(st.hasMoreTokens()) {
			strArr[i++] = st.nextToken();
		}
		return strArr;
	}
	
	static String[] readStrArr() throws Exception {
		while(true) {
			String line = readLine();
			if(line == null) 
				return null;
			else if(line.equals(""))
				continue;
			return toStrArr(line);
		}
	}
	
	static String readLine() throws Exception {		
		return br.readLine();
	}
	
	static boolean isEmpty(String s) {
		return (s == null || s.equals(""));
	}
	
	static void flush() {
		System.out.print(outputBuffer.toString());
		outputBuffer = new StringBuilder(10000);
	}
	
	static void print(int n) {
		outputBuffer.append(n);
	}
	
	static void print(long n) {
		outputBuffer.append(n);
	}
	
	static void print(char ch) {
		outputBuffer.append(ch);
	}
	
	static void print(String s) {
		outputBuffer.append(s);
	}
	
	static void println() {
		outputBuffer.append("\n");
	}
	
	static void println(int n) {
		outputBuffer.append(n + "\n");
	}
	
	static void println(long n) {
		outputBuffer.append(n + "\n");
	}
	
	static void println(String s) {
		outputBuffer.append(s + "\n");
	}
	
	static int max(int a, int b) {
		return (a>=b ? a : b);
	}
	
	static int min(int a, int b) {
		return (a<b ? a : b);
	}
	
	static long abs(long n) {
		return Math.abs(n);
	}
	
	static void myassert(boolean b) throws Exception {
		if(!b)
			throw new Exception("BAD");
	}
	
	class TeamStat implements Comparable<TeamStat> {
		public String name = null;
		public int played = 0;
		public int wins = 0;
		public int ties = 0;
		public int losses = 0;
		public int goalsScored = 0;
		public int goalsAgainst = 0; 
		
		public TeamStat(String _name) {
			name = _name;
		}
		
		public int TotalPoints() {
			return 3*wins + 1*ties;
		}
		
		public int GoalDifference() {
			return goalsScored - goalsAgainst;			
		}
		
		public int compareTo(TeamStat ts2) {
			int totalPoints = TotalPoints();
			int totalPoints2 = ts2.TotalPoints();
			int goalDifference = GoalDifference();
			int goalDifference2 = ts2.GoalDifference();
			if(totalPoints > totalPoints2)
				return -1;
			else if(totalPoints < totalPoints2)
				return 1;
			else if(wins > ts2.wins)
				return -1;
			else if(wins < ts2.wins)
				return 1;
			else if(goalDifference > goalDifference2)
				return -1;
			else if(goalDifference < goalDifference2)
				return 1;
			else if(goalsScored > ts2.goalsScored)
				return -1;
			else if(goalsScored < ts2.goalsScored)
				return 1;
			else if(played < ts2.played)
				return -1;
			else if(played > ts2.played)
				return 1;
			
			return (name.toUpperCase().compareTo(ts2.name.toUpperCase()));
		}
	}
	
	public static void main(String args[]) throws Exception {
		int C = readInt();
		Main m = new Main();
		for(int c=1; c<=C; c++) {
			String tournament = readLine();
			
			ArrayList<TeamStat> stats = new ArrayList<TeamStat>();
			HashMap<String, Integer> teams = new HashMap<String, Integer>();
			
			int T = readInt();
			for(int t=0; t<T; t++) {
				String teamname = readLine();
				stats.add(m.new TeamStat(teamname));
				teams.put(teamname, t);
			}
			
			int G = readInt();
			for(int g=0; g<G; g++) {
				String game = readLine();
				String strArr[] = game.split("#");
				int teamIndex = teams.get(strArr[0]);				
				int team2Index = teams.get(strArr[2]);
				myassert(teamIndex != team2Index);
				String result[] = strArr[1].split("@");
				int goals = toInt(result[0]);
				int goals2 = toInt(result[1]);
				
				TeamStat team = stats.get(teamIndex);
				TeamStat team2 = stats.get(team2Index);
				team.played++;
				team2.played++;
				team.goalsScored += goals;
				team.goalsAgainst += goals2;
				team2.goalsScored += goals2;
				team2.goalsAgainst += goals;
				if(goals > goals2) {
					team.wins++;
					team2.losses++;
				} else if(goals < goals2) {
					team.losses++;
					team2.wins++;
				} else {
					team.ties++;
					team2.ties++;
				}
			}
			
			Collections.sort(stats);
			
			if(c>1)
				println();
			
			println(tournament);
			int ranking = 1;
			for(TeamStat team : stats) {
				print(ranking);
				print(") " + team.name + " " + team.TotalPoints() + "p, " + team.played + "g ");
				print("(" + team.wins + "-" + team.ties + "-" + team.losses + "), ");
				println(team.GoalDifference() + "gd (" + team.goalsScored + "-" + team.goalsAgainst + ")");
				ranking++;
			}
			flush();
		}
	}
}
JohnTortugo
New poster
Posts: 18
Joined: Sun Jul 20, 2008 7:05 pm

Re: 10194 - Football (aka Soccer)

Post by JohnTortugo »

Hi guys, i'm getting lots of W.A in this problem, i really dont know what's wrong with my code. I'll be glad by any help.

Thanks in advance.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <ctype.h>
#include <algorithm>
#include <map>
#include <utility>
#include <iostream>

#define TRACE(x...)
#define PRINT(x...) TRACE(printf(x))
#define WATCH(x) TRACE(cout << #x" = " << x << "\n")
#define _inline(f...) f() __attribute__((always_inline)); f
#define _foreach(it, b, e) for (typeof(b) it = (b); it != (e); it++)
#define foreach(x...) _foreach(x)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

const int INF = 0x3F3F3F3F;
const int NULO = -1;
const double EPS = 1e-10;

using namespace std;

/*--------------------------------------------------------------*/

#define 	MAX_EQUIPE_NAME 	150
#define		MAX_TIMES			35

typedef struct {
		char nome[MAX_EQUIPE_NAME];
		int oleitura;
		int pontos;
		int vitorias;
		int derrotas;
		int empates;
		int gols_pros;
		int gols_cont;
} equipe;

equipe * initTime(string nomeTime, int order);
int cmpScore(const void *timea, const void *timeb);
void strToUpper(char *str);

_inline(int cmp)(double x, double y = 0, double tol = EPS) {
	return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

int main() {

	int ncamps;		/* numero de campeonatos */
	int ntimes;		/* numero de times */
	int njogos;		/* numero de jogos */
	int golsa, golsb;	/* gols marcados por a e por b */
	int rank;		/* rank do time */
	
	string nomeTime;
	string nomeCamp;
	
	map<string, equipe *> times;
	map<string, equipe *>::iterator it;

	equipe resultado[MAX_TIMES];
	
	char equipea[MAX_EQUIPE_NAME], equipeb[MAX_EQUIPE_NAME];

	cin >> ncamps;
	cin.ignore();
	
	for (int i=0; i<ncamps; i++) {
		times.clear();

		getline(cin, nomeCamp);		/* nome do campeonato */
		//cout << "Nome Camp: " << nomeCamp << endl;
		
		cin >> ntimes;	/* quantos times */
		cin.ignore();
		//cout << "Num times: " << ntimes << endl;
		
		for (int j=0; j<ntimes; j++) {
			getline(cin, nomeTime);
			times[nomeTime] = initTime(nomeTime, j);
			//cout << "\t" << nomeTime << endl;
		}
	
		cin >> njogos;	/* quantos jogos jogados */
		cin.ignore();
		//cout << "Jogos: " << njogos << endl;

		for (int k=0; k<njogos; k++) {
			scanf("%[^#]#%d@%d#%[^\n]\n", equipea, &golsa, &golsb, equipeb);
			//printf("\t%s %d --- %s %d\n", equipea, golsa, equipeb, golsb);

			equipe *timea = times[equipea];
			equipe *timeb = times[equipeb];

			if (golsa > golsb) {	/* time-1 ganhou */
				timea->pontos += 3;
				timea->vitorias += 1;
				timea->gols_pros += golsa;
				timea->gols_cont += golsb;

				timeb->derrotas += 1;
				timeb->gols_pros += golsb;
				timeb->gols_cont += golsa;
			}
			else if (golsa < golsb) {	/* time-2 ganhou */
				timeb->pontos += 3;
				timeb->vitorias += 1;
				timeb->gols_pros += golsb;
				timeb->gols_cont += golsa;

				timea->derrotas += 1;
				timea->gols_pros += golsa;
				timea->gols_cont += golsb;
			}
			else {	/* empate */
				timea->pontos += 1;
				timeb->pontos += 1;

				timea->empates += 1;
				timeb->empates += 1;

				timea->gols_pros += golsa;
				timea->gols_cont += golsb;

				timeb->gols_pros += golsb;
				timeb->gols_cont += golsa;
			}
		}

		/* copia os dados para um vetor, para ordena-los */
		for (it=times.begin(), rank=0; it!=times.end(); it++, rank++) {
				equipe *time = it->second;
				resultado[rank] = *time;
		}

		qsort(resultado, ntimes, sizeof(equipe), cmpScore);

		/* Imprime o ranking */
		printf("%s\n", nomeCamp.c_str());
		for (rank=0; rank<ntimes; rank++) {
				equipe time = resultado[rank];
				printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", rank+1, time.nome, time.pontos, time.derrotas+time.empates+time.vitorias, time.vitorias, time.empates, time.derrotas, time.gols_pros - time.gols_cont, time.gols_pros, time.gols_cont);		
		}
		printf("\n");
	}

	return 0;
}

equipe * initTime(string nomeTime, int order) {
	equipe *t = (equipe *) malloc(sizeof(equipe));
	 	 
	strcpy(t->nome, nomeTime.c_str());
	t->oleitura = order;
	t->pontos = 0;
	t->vitorias = 0;
	t->derrotas = 0;
	t->empates = 0;
	t->gols_pros = 0;
	t->gols_cont = 0;
	
	return t;
}

void strToLower(char *str) {
	 std::transform(str, str+sizeof(str)/sizeof(char), str, (int(*)(int)) tolower);
}

int cmpScore(const void *timea, const void *timeb) {
	equipe *ea = (equipe *) timea;
	equipe *eb = (equipe *) timeb;

	int difa = ea->gols_pros - ea->gols_cont;
	int difb = eb->gols_pros - eb->gols_cont;

	int ja = ea->vitorias + ea->derrotas + ea->empates;
	int jb = eb->vitorias + eb->derrotas + eb->empates;

	char nomea[MAX_EQUIPE_NAME];
	char nomeb[MAX_EQUIPE_NAME];

	if (ea->pontos != eb->pontos) return eb->pontos - ea->pontos;

	if (ea->vitorias != eb->vitorias) return eb->vitorias - ea->vitorias;

	if (difa != difb) return difb - difa;

	if (ea->gols_pros != eb->gols_pros) return eb->gols_pros - ea->gols_pros;

	if (ja != jb) return ja-jb;

	strcpy(nomea, ea->nome);
	strcpy(nomeb, eb->nome);

	strToLower(nomea);
	strToLower(nomeb);

	return strcmp(nomea, nomeb);
}
JohnTortugo
New poster
Posts: 18
Joined: Sun Jul 20, 2008 7:05 pm

Re: 10194 - Football (aka Soccer)

Post by JohnTortugo »

No one know what is wrong with my code? I think it's a little mistake...

Thanks.
L I M O N
Learning poster
Posts: 58
Joined: Wed Dec 31, 2003 8:43 am
Location: Dhaka, Bangladesh
Contact:

Re: 10194 - Football (aka Soccer)

Post by L I M O N »

find your bug here : http://www.youngprogrammer.com
JohnTortugo
New poster
Posts: 18
Joined: Sun Jul 20, 2008 7:05 pm

Re: 10194 - Football (aka Soccer)

Post by JohnTortugo »

Hi all.

thanks LIMON. I found my error, it was a "\n" at the end of output. I used only strcasecmp....

But now I'm confused... this shouldn't be a Presentation Error (the "\n" at the end)?

thanks. John.
ergysr
New poster
Posts: 9
Joined: Sun Oct 07, 2007 1:25 pm

Re: 10194 - Football (aka Soccer)

Post by ergysr »

Be careful, it's not only the case insensitive comparison to mind. There should be a blank line BETWEEN each case. So a '\n' after the last case should be avoided as it gives WA.
assasin
New poster
Posts: 7
Joined: Wed Feb 13, 2008 3:59 pm
Location: Dhaka,Bangladesh

Re: 10194 - Football (aka Soccer)

Post by assasin »

I do not understand what to do with this problem. Getting WA continuously.
Here goes my code

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


typedef struct{
	char name[32];
	int point;
	int numGame;
	int numWin;
	int numTie;
	int numLoss;
	int goalScored;
	int goalAgainst;
}team;

int compare_function(const void *a,const void *b){
  	 	
  	 team *x = (team *) a;
  	 team *y = (team *) b;
  	 
  	 	
  	 if( x->point != y->point ){
  	 	//printf("For points between %s and %s\n",x->name,y->name);
  	 	return y->point - x->point;
  	 }
  	 else if (x->numWin != y->numWin){
  	 	//printf("For wins between %s and %s\n",x->name,y->name);
  	 	return y->numWin - x->numWin;	
  	 }
  	 else if( (x->goalScored-x->goalAgainst) != (y->goalScored - y->goalAgainst) ){	
  	 	//printf("For goalDifference between %s and %s\n",x->name,y->name);
  	 	return (y->goalScored-y->goalAgainst) - (x->goalScored - x->goalAgainst);
  	 }
  	 else if( x->goalScored != y->goalScored){
  	 	//printf("For goal Scored between %s and %s\n",x->name,y->name);
  	 	return y->goalScored - x->goalScored;
  	 }
  	 else if( x->numGame != y->numGame ){
  	 	//printf("For number of game between %s and %s\n",x->name,y->name);
  	 	return x->numGame - y->numGame;
  	 }
  	 else{ 
  	 	int i;
  	 	char first[32],second[32];
  	 	
  		for( i = 0 ; i < strlen(x->name) ; i++ )
  	 		first[i] = tolower(x->name[i]);	
  	 			
  	 		
  	 	for( i = 0 ; i < strlen(y->name) ; i++ )
  	 		second[i] = tolower(y->name[i]);		
  	 	
  	 	//printf("For lex order between %s and %s\n",x->name,y->name);
  	 	
  	 	return !strcmp(first,second);
  	 }
}

int ConvertInt(char *str){

	int i,j,len = strlen(str),k=0,sum=0;
	
	for( i = len - 1 ; i >= 0 ; i--){
		
		int temp = 1;
		
		for( j = 0 ; j < k ; j++ )
			temp *= 10;
			
		sum += temp * (str[i]-'0');
		k++;
	}	

	return sum;
}

int main(){
	
	int numTournament;			//0<N<1000
	char TournamentName[102];	// < 100 char
	int numTeam;				//1 < T <= 30 , then T lines
	int numMatch;
	team *Team; 	
  	int cases,count=0;
  	int i,j;
  	char Team1[32],Team2[32],Goal1[3],Goal2[3];
  	int NumGoal1,NumGoal2;
  	
  	
  	scanf("%d",&cases);

  	
  	while(count++ < cases){
  	
  		if(count > 1)
  			printf("\n");
  	
  		scanf("%*c%[^\n]",TournamentName);
  		scanf("%*c%d%*c",&numTeam);
  		
  		
  		//printf("%s %d\n",TournamentName,numTeam);
  		
  		
  		Team = new team[numTeam];
  		
  		for( i = 0 ; i < numTeam ; i++){
  			scanf("%[^\n]%*c",Team[i].name);
  			
  			Team[i].point = Team[i].numGame = Team[i].numWin = Team[i].numTie = Team[i].numLoss = Team[i].goalScored = 
  			Team[i].goalAgainst = 0;	
  		}
  		
  		scanf("%d",&numMatch);
  		
  		for( i = 0 ; i < numMatch ; i++ ){
  			scanf("%*c%[^#]%*c%[0-9]%*c%[0-9]%*c%[^\n]",Team1,Goal1,Goal2,Team2);
  			
  			NumGoal1 = ConvertInt(Goal1);
			NumGoal2 = ConvertInt(Goal2);
  			
  			//printf("Team1 %s, goal %d,Team2 %s, goal %d\n",Team1,NumGoal1,Team2,NumGoal2);
			
			 for( j = 0 ; j < numTeam ; j++){
			 
			 	if(!strcmp(Team1,Team[j].name)){
			 		Team[j].goalScored += NumGoal1; Team[j].goalAgainst += NumGoal2; Team[j].numGame++;
			 		if(NumGoal1 > NumGoal2)
			 			Team[j].numWin++,Team[j].point+=3;
			 		else if(NumGoal1 == NumGoal2)
			 			Team[j].numTie++,Team[j].point+=1;
			 		else Team[j].numLoss++;
			 		
			 	}
			 	
			 	else if(!strcmp(Team2,Team[j].name)){
			 		Team[j].goalScored += NumGoal2; Team[j].goalAgainst += NumGoal1; Team[j].numGame++;
			 		if(NumGoal2 > NumGoal1)
			 			Team[j].numWin++,Team[j].point+=3;
			 		else if(NumGoal1 == NumGoal2)
			 			Team[j].numTie++,Team[j].point+=1;
			 		else Team[j].numLoss++;
			 		
			 	}
			 	else;
			 } 		
  		}
  		
  		
  		qsort(Team,numTeam,sizeof(team),compare_function);
    	
    	
    		printf("%s\n",TournamentName);
    	
    		for( i = 0 ; i < numTeam ; i++)
    			printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,Team[i].name,Team[i].point,Team[i].numGame,Team[i].numWin,Team	[i].numTie,Team[i].numLoss,Team[i].goalScored-Team[i].goalAgainst,Team[i].goalScored,Team[i].goalAgainst);
    			
  			
 	
  	} 	
    				

	return 0;
}
Please someone help to find my problem and give some critical I/O
Thanks.
Post Reply

Return to “Volume 101 (10100-10199)”