공부하기/백준

[Java] 백준 풀기 11645 - I’ve Been Everywhere, Man

XEV 2024. 3. 7. 21:15

자바 백준 11645번

실버 5

https://www.acmicpc.net/problem/11645

 

11645번: I’ve Been Everywhere, Man

The first line of input contains a single positive integer T ≤ 50 indicating the number of test cases. The first line of each test case also contains a single positive integer n indicating the number of work trips Alice has taken so far. The following n

www.acmicpc.net

 

 

 

 

 

문제 보기

분류: 자료 구조, 문자열, 해시를 사용한 집합과 맵

 

 

 

 

 

코드 풀이

import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 테스트 케이스의 수 입력
int T = sc.nextInt();
// 각 테스트 케이스에 대해 처리
for (int t = 0; t < T; t++) {
// 여행 횟수 입력
int n = sc.nextInt();
// AliceTripCounter 객체 생성
AliceTripCounter tripCounter = new AliceTripCounter();
// 각 여행에 대해 처리
for (int i = 0; i < n; i++) {
// 도시 이름 입력
String city = sc.next();
// 도시를 여행 기록에 추가
tripCounter.addTrip(city);
}
// 여행한 도시의 수 출력
System.out.println(tripCounter.countDistinctCities());
}
}
}
// Alice의 여행 기록을 관리하는 클래스
class AliceTripCounter {
// 도시 방문 기록을 저장하는 해시맵
private HashMap<String, Integer> visitedCities;
// constructor
public AliceTripCounter() {
this.visitedCities = new HashMap<>();
}
// 새로운 여행을 기록하는 메서드
public void addTrip(String city) {
// 해당 도시를 방문한 것으로 기록
visitedCities.put(city, 1);
}
// 여행한 도시의 수를 반환하는 메서드
public int countDistinctCities() {
// 방문한 도시의 수 반환
return visitedCities.size();
}
}