๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 1764๋ฒˆ: ๋“ฃ๋ณด์žก(๊ตฌํ˜„, ์ •๋ ฌ) - HashSet

by ์ฃผ๋ฐœ2 2020. 3. 4.
๋ฐ˜์‘ํ˜•

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

 

1764๋ฒˆ: ๋“ฃ๋ณด์žก

์ฒซ์งธ ์ค„์— ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ N, ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„๊ณผ, N+2์งธ ์ค„๋ถ€ํ„ฐ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ด๋ฆ„์€ ๋„์–ด์“ฐ๊ธฐ ์—†์ด ์˜์–ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง€๋ฉฐ, ๊ทธ ๊ธธ์ด๋Š” 20 ์ดํ•˜์ด๋‹ค. N, M์€ 500,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

ํ‹€๋ฆฐ ์ฝ”๋“œ

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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		String[] nArr = new String[N];	// ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
		String[] mArr = new String[M];	// ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
		String[] nmArr = new String[500000];	// ๋“ฃ๋„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
		int index = 0;
		int count = 0;
		
		// ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ž…๋ ฅ
		for(int i=0; i<nArr.length; i++) 
			nArr[i] = bf.readLine();
		// ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ž…๋ ฅ
		for(int i=0; i<mArr.length; i++)
			mArr[i] = bf.readLine();
		
		Arrays.sort(nArr);
		Arrays.sort(mArr);
		
//		for(int i=0; i<N; i++)
//			System.out.print(nArr[i] + " ");
//		System.out.println();
//		for(int i=0; i<M; i++)
//			System.out.print(mArr[i] + " ");
		
		for(int i=0; i<nArr.length; i++) {
			for(int j=0; j<mArr.length; j++) {
				if(nArr[i].equals(mArr[j])) {	// ๋“ฃ๋„ ๋ณด๋„ ๋ชปํ–ˆ์„๋•Œ => ์นด์šดํŠธ +1 , ์ €์žฅ
					count ++;
					nmArr[index ++] = nArr[i];
					break;
				}
			}
		}
		
		
		
		System.out.println(count);
		for(int i=0; i<nmArr.length; i++) {
			if(nmArr[i] == null)	break;
			System.out.println(nmArr[i]);
		}
		bf.close();
	}

}

ํ’€์ด

๊ฐ€์žฅ ๋ฌด๋‚œํ•˜๊ฒŒ ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ๋ช…๋‹จ - String nArr[] , ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ๋ช…๋‹จ - String mArr[] ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ณ ,

๋“ฃ๋„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ๋ช…๋‹จ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด String nmArr[] ์„ ์„ ์–ธํ–ˆ๋‹ค.

๊ทธ ํ›„ ๊ฐ ๋ฐฐ์—ด์— ์™„์ „ํƒ์ƒ‰์„ ํ†ตํ•ด 2์ค‘ for๋ฌธ์„ ๋Œ๋ ธ์œผ๋‚˜ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

์‚ฌ์‹ค ๋ฌธ์ œ์—์„œ N, M์˜ ๋ฒ”์œ„๋Š” 500,000 ์ด์–ด์„œ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์ „์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ• ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐ์€ ํ–ˆ์—ˆ์ง€๋งŒ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

๋‘ String ์ค‘ ํ•œ String์ด ํฌํ•จ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ•  ์ง€ ๊ณ ๋ฏผํ–ˆ์ง€๋งŒ ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋‹ค.

๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์•„์ง ๋ถ€์กฑํ•˜์—ฌ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

์ •๋‹ต ์ฝ”๋“œ

import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int N = Integer.parseInt(st.nextToken());	// ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜
		int M = Integer.parseInt(st.nextToken());	// ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜
		
		Set<String> hs = new HashSet<String>();	// ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
		List<String> list = new ArrayList<String>();
		for(int i=0; i<N; i++)	
			hs.add(bf.readLine());	// ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
		
		for(int i=0; i<M; i++) {
			String str = bf.readLine();
			// ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์ด ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์— ์žˆ์„๊ฒฝ์šฐ => list์— ์ €์žฅ
			if(hs.contains(str)) 	list.add(str);
		}
		
		Collections.sort(list);
		System.out.println(list.size());	// ๋“ฃ๋„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ => list์˜ ํฌ๊ธฐ
		for(int i=0; i<list.size(); i++)
			System.out.println(list.get(i));
		
		bf.close();
	}

}

ํ’€์ด

HastSet์„ ์ด์šฉํ•ด ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ง‘๋‹จ์— ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์ด ํฌํ•จ๋˜๋Š”์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

 - HasbSet.contains() 

์ •๋‹ต์€ List์— ๋‹ด์•„์„œ ์ €์žฅํ•œ๋‹ค.

 

๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์„ ์ž…๋ ฅ๋ฐ›๊ณ  HashSet์— ์ €์žฅํ•œ๋‹ค. => hs.add()

๊ทธ ํ›„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋งˆ๋‹ค, ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์„ ์ €์žฅํ•œ HashSet์— ์žˆ๋Š”๊ฒฝ์šฐ List์— ์ €์žฅํ•œ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ ๋ช…๋‹จ์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ, List๋ฅผ ์ •๋ ฌํ•˜๊ณ  ๋“ฃ๋ณด์žก์˜ ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ์ธ size()๊ฐ€ ๋œ๋‹ค.

 

์œ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ˜„์ €ํžˆ ์ค„์–ด๋“ ๋‹ค.

์ด์ „ ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ์—๋Š” O(500,000 * 500,000) ์˜€๋‹ค๋ฉด ์œ„๋Š” for๋ฌธ์ด 1๊ฐœ์ด๋ฏ€๋กœ O(M)์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 * ๋‹ค์–‘ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๋” ์ต์ˆ™ํ•ด์ ธ์•ผํ•  ๊ฒƒ ๊ฐ™๊ณ , ์‹œ๊ฐ„๋ณต์žก๋„์— ๋Œ€ํ•ด ์ข€ ๋” ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€