본문 바로가기

문제 풀이/Baekjoon

[백준] S5 15233번 Final Score (JAVA)

문제 출처 - Baekjoon Online Judge

문제는 여기

 

15233번: Final Score

We have had a problem with one of our hard disks and we lost the final score of some football matches. However, we have been able to recover the names of the players that scored and found the members of each team on Wikipedia.

www.acmicpc.net

[문제] 

We have had a problem with one of our hard disks and we lost the final score of some football matches. However, we have been able to recover the names of the players that scored and found the members of each team on Wikipedia.

[입력]

The first line will contain three integers A, B and G representing the number of players in team A and team B and the number of goals scored in the match.

The second line will contain A strings separated by spaces. The names of the players of team A.

The third line will contain B strings separated by spaces. The names of the players of team B.

The fourth line will contain G strings. The names of the players that scored.

There will be at most 20 players in each team and at most 100 goals.

The names of the players are lowercase latin letters and they will have at most 20 characters each.

[출력]

Print "A" if the team A scored more than team B.

Print "B" if the team B scored more than team A.

Print "TIE" if both scored the same number of goals.

 

 


[풀이]

1. A팀의 멤버, B팀의 멤버를 순서대로 입력받아 해쉬맵에 저장한다. (<이름, 팀>으로 저장했다.)

2. G번의 사람 이름을 입력받아 해당하는 팀에 맞게 카운터를 증가시켜 결과를 비교하고 출력해준다.

[접근]

1. A팀의 멤버 이름과 B팀의 멤버이름을 입력받아 G번의 선수 이름을 입력받아 누가 승리했는지 결과를 출력하는 문제였다.

2. 해쉬맵에 이름과 팀으로 저장하고 값을 구하면 되겠다고 생각하고 문제를 해결하였다.

[코드]

package BOJ_silver;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main_S5_15233 {
	static HashMap<String, String> map;
	static int a;
	static int b;
	static int g;
	static int cnt;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		a = Integer.parseInt(st.nextToken());
		b = Integer.parseInt(st.nextToken());
		g = Integer.parseInt(st.nextToken());
		
		map = new HashMap<>();
		
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < a; i++) {
			String name = st.nextToken();
			
			map.put(name, "A");
		}
		
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < b; i++) {
			String name = st.nextToken();
			
			map.put(name, "B");
		}
		
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < g; i++) {
			String name = st.nextToken();
			
			if (map.get(name) == "A")
				cnt++;
		}
		
		if (cnt > g - cnt)
			System.out.println("A");
		else if (cnt < g - cnt)
			System.out.println("B");
		else if (cnt == g - cnt)
			System.out.println("TIE");
	}
}