|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + static int N,M; |
| 10 | + static int[] A,B; |
| 11 | + |
| 12 | + |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + N = Integer.parseInt(br.readLine()); |
| 15 | + A = new int[N]; |
| 16 | + st = new StringTokenizer(br.readLine()); |
| 17 | + for (int i = 0; i < N; i++) { |
| 18 | + A[i] = Integer.parseInt(st.nextToken()); |
| 19 | + } |
| 20 | + |
| 21 | + M = Integer.parseInt(br.readLine()); |
| 22 | + B = new int[M]; |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + for (int i = 0; i < M; i++) { |
| 25 | + B[i] = Integer.parseInt(st.nextToken()); |
| 26 | + } |
| 27 | + |
| 28 | + List<Integer> common = new ArrayList<>(); |
| 29 | + int[] temp = searchMax(-1,-1); |
| 30 | + while(temp[0] != -1) { |
| 31 | + common.add(A[temp[0]]); |
| 32 | + temp = searchMax(temp[0], temp[1]); |
| 33 | + } |
| 34 | + if(common.size() == 0){ |
| 35 | + bw.write("0"); |
| 36 | + }else{ |
| 37 | + bw.write(common.size()+"\n"); |
| 38 | + for(int i:common){ |
| 39 | + bw.write(i+" "); |
| 40 | + } |
| 41 | + } |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + public static int[] searchMax(int endA, int endB){ |
| 45 | + int maxAIndex = -1 ,maxBIndex = -1; |
| 46 | + for (int i = N-1; i > endA; i--) { |
| 47 | + for (int j = M-1; j > endB; j--) { |
| 48 | + if (A[i] == B[j]){ //공통으로 가지는 경우 |
| 49 | + if(maxAIndex == -1 || A[maxAIndex] <= A[i]){ |
| 50 | + maxAIndex = i; |
| 51 | + maxBIndex = j; |
| 52 | + }else{ |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return new int[]{maxAIndex,maxBIndex}; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +``` |
0 commit comments