728x90
300x250
https://www.acmicpc.net/problem/1764
백준 1764번: 듣보잡 [자료구조][정렬][문자열] -Java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//System.setIn(new FileInputStream("Test.txt"));
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());
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
String key = st.nextToken();
int cnt = 0;
if(map.get(key)!=null)
cnt = map.get(key);
map.put(key, cnt+1);
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
String key = st.nextToken();
int cnt = 0;
if(map.get(key)!=null)
cnt = map.get(key);
map.put(key, cnt+1);
}
ArrayList<String> list = new ArrayList<String>();
for(String key : map.keySet()) {
if(map.get(key)>1)
list.add(key);
}
Collections.sort(list);
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
728x90
'알고리즘 > 그 외' 카테고리의 다른 글
백준 15828번: Router [큐][자료구조][Queue] -Java (2) | 2023.11.20 |
---|---|
백준 10845번: 큐 [큐][Queue][자료구조] -Java (0) | 2023.11.13 |
백준 2503번: 숫자 야구 [브루트포스][구현] - Java (1) | 2023.10.18 |
백준 1406번: 에디터 [자료구조][리스트] -Java (0) | 2023.09.30 |
백준 1931번: 회의실 배정 [정렬][그리디][Greedy] -Java (0) | 2023.07.31 |