알고리즘/해시

[백준] 1620번 나는야 포켓몬 마스터 이다솜 #Java

VIPeveloper 2022. 5. 15. 23:57
728x90
반응형
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        String [] arr = new String[N+1];
        HashMap<String,Integer> hashMap = new HashMap<>();

        for (int i = 1; i <= N; i++) {
            arr[i] = br.readLine();
            hashMap.put(arr[i],i);
        }
        for (int i = 0; i < M; i++) {
            String s = br.readLine();
            if(isNumber(s)){
                int i1 = Integer.parseInt(s);
                System.out.println(arr[i1]);
            }else{
                System.out.println(hashMap.get(s));
            }
        }
    }
    private static boolean isNumber(String s) {
        try {
            int i = Integer.parseInt(s);
        }catch (NumberFormatException e){
            return false;
        }
        return true;
    }
}
728x90
반응형