package Baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_1764 {
public static void main(String[] argv) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
String Nli[] = new String[N];
String Nse[] =new String[M];
for(int i=0;i<N;i++){
Nli[i] = br.readline;
}
for(int i=0;i<M;i++){
Nse[i] = br.readline;
}
ArrayList<String> NAll =new ArrayList<>();
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(Nli[i].equals(Nse[j])) NAll.add(Nli[i]);
}
}
Collections.sort(NAll);
System.out.println(NAll.size());
for(int i=0;i<NAll.size();i++){
System.out.println(NAll.get(i));
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_1764 {
public static void main(String[] argv) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Set<String> set = new HashSet<>(); //set.contain()을 쓰기 위함.
ArrayList<String> NAll =new ArrayList<>(); //그리고 여기다 추가.
for(int i=0;i<N;i++){
set.add(br.readLine());
}
for(int i=0;i<M;i++){
String str = br.readLine();
if(set.contains(str)) NAll.add(str);
}
Collections.sort(NAll);
sb.append(NAll.size()).append("\n");
for(int i=0;i<NAll.size();i++){
sb.append(NAll.get(i)).append("\n");
}
System.out.println(sb);
}
}
참조:
https://zzang9ha.tistory.com/146,
http://daplus.net/java-stringbuilder%EC%97%90-%EA%B0%9C%ED%96%89%EC%9D%84-%EC%B6%94%EA%B0%80%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95/