🐣 알고리즘 삐약/💻 백준 삐약

102 삐약 : 백준 13414 | 수강신청 [바킹독| HASH |JAVA]

우주수첩 2024. 10. 31. 16:26
728x90

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

 

package BKD_0x15_Hash;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;

public class BOJ_13414 {
    public static void main(String[] args) throws IOException {
        LinkedHashSet<String> set = new LinkedHashSet<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int K = Integer.parseInt(st.nextToken());
        int L = Integer.parseInt(st.nextToken());

        for(int i=0;i<L;i++){
            String str = br.readLine();
            if(set.contains(str)){
                set.remove(str);
            }
            set.add(str);
        }


        for(String str : set){
            if(K--==0) break;
            System.out.println(str);

        }


    }
}

 

  • 순서가 저장되어있는 HashSet사용.
  • LinkedHashSet<> set = new LinkedHashSet<>();

 

메소드 맨날 헷갈림 꾸엥

  • Set : Contains, remove, add
  • Map : ContainsKey, ContainsValue, remove, put

 

 

728x90