SWEA 2948 문자열 교집합

1 개요[ | ]

SWEA 2948 문자열 교집합
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 3-5

2 C++[ | ]

3 Java[ | ]

import java.util.*;
public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1; tc<=T; tc++) {
			int N = sc.nextInt();
			int M = sc.nextInt();
			PriorityQueue<String> a = new PriorityQueue<String>();
			PriorityQueue<String> b = new PriorityQueue<String>();
			for(int i=0; i<N; i++) a.add(sc.next());
			for(int i=0; i<M; i++) b.add(sc.next());
			int count = 0;
			while(true) {
				if(a.isEmpty() || b.isEmpty()) break;
				String str1 = a.peek();
				String str2 = b.peek();
				int c = str1.compareTo(str2);
				if( c < 0 ) {
					a.poll();
					continue;
				}
				if( c > 0 ) {
					b.poll();
					continue;
				}
				count++;
				a.poll();
				b.poll();
			}
			System.out.format("#%d %d\n", tc, count);
		}
		sc.close();
	}
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}