Deep_Lee

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

 

1620๋ฒˆ: ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด

www.acmicpc.net

 


์‚ฌ์‹ค ๋‹จ์ˆœํ•˜๊ฒŒ, ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ index ๋ฐ˜ํ™˜

์ˆซ์ž(index)๋กœ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ํ•ด๋‹น index์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ฒ˜์Œ์—” ๊ทธ๋ƒฅ ArrayList์—์„œ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์„ ํ–ˆ๋”๋‹ˆ, ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

๊ทธ๋ž˜์„œ map๊ณผ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ํ‘ธ๋Š” ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.

 

์ฝ”๋“œ๋Š”, ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ์‹คํŒจํ–ˆ๋˜ ์ฝ”๋“œ์™€

์„ฑ๊ณตํ•œ ์ฝ”๋“œ ๋ชจ๋‘ ์ฒจ๋ถ€ํ•œ๋‹ค.

 

 

 

1. ArrayList๋กœํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋˜ ์ฝ”๋“œ

import java.util.*;

class Main {
    public static void main(String args[]) throws Exception {
        Scanner kb = new Scanner(System.in);
        int n=kb.nextInt();
        int m=kb.nextInt();

        ArrayList<String> list=new ArrayList<>();
        for(int i=0; i<n; i++) list.add(kb.next());

        for(int i=0; i<m; i++){
           String temp=kb.next();
           if(list.contains(temp)){
               System.out.println((list.indexOf(temp))+1);
           }else{
               System.out.println((list.get(Integer.parseInt(temp)-1)));
           }
        }

    }
}

 

 

2. Map + ๋ฐฐ์—ด๋กœ ์„ฑ๊ณตํ•œ ์ฝ”๋“œ

import java.util.*;

class Main {
    public static void main(String args[]) throws Exception {
        Scanner kb = new Scanner(System.in);
        int n=kb.nextInt();
        int m=kb.nextInt();

        HashMap<String, Integer> map = new HashMap<>();
        String[] arr=new String[n];
        for(int i=0; i<n; i++){
            String s=kb.next();
            arr[i]=s;
            map.put(s,i+1);
        }
        for(int i=0; i<m; i++){
            if(kb.hasNextInt()) System.out.println(arr[kb.nextInt()-1]);
            else System.out.println(map.get(kb.next()));
        }
    }
}