gun0680   2년 전

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class BOJ9252 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();
    static int[][] dp;
    static char[] str1, str2;

    public static void main(String[] args) throws IOException {
        str1 = br.readLine().toCharArray();
        str2 = br.readLine().toCharArray();
        dp = new int[str1.length + 1][str2.length + 1];

        for (int i = 1; i <= str1.length; i++) {
            for (int j = 1; j <= str2.length; j++) {
                if (str1[i - 1] == str2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        sb.append(dp[str1.length][str2.length]).append("\n");

        Stack<Character> stack = new Stack<>();
        for (int i = str1.length, j = str2.length; i > 0 && j > 0; ) {
            if (dp[i][j] == dp[i - 1][j]) {
                i--;
            } else if (dp[i][j] == dp[i][j - 1]) {
                j--;
            } else {
                stack.push(str1[i - 1]);
                i--;
                j--;
            }
        }
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        
        System.out.println(sb);
    }
}


/////////////////////////////////////////////////////////////////////////////////////////

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();
    static int[][] dp;
    static char[] str1, str2;

    public static void main(String[] args) throws IOException {
        str1 = br.readLine().toCharArray();
        str2 = br.readLine().toCharArray();
        dp = new int[str1.length + 1][str2.length + 1];

        for (int i = 1; i <= str1.length; i++) {
            for (int j = 1; j <= str2.length; j++) {
                if (str1[i - 1] == str2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        for (int i = str1.length, j = str2.length; i > 0 && j > 0; ) {
            if (dp[i][j] == dp[i - 1][j]) {
                i--;
            } else if (dp[i][j] == dp[i][j - 1]) {
                j--;
            } else {
                sb.append(str1[i - 1]);
                i--;
                j--;
            }
        }
        if (sb.length() != 0) sb.append("\n");
        sb.append(dp[str1.length][str2.length]);
        sb.reverse();
        System.out.println(sb);
    }
}

StringBuilder의 reverse를 사용해서 찾아낸 LCS문자열을 뒤집으면서 나머지도 한번에 뒤집어서 출력을 하려고 했습니다.

0일때도 고려해서 개행문자가 포함되지 않도록 처리도 해주고 출력도 정상적으로 됩니다..

알고리즘에 문제가 있나해서 검토를 수십번을 했는데 Stack를 사용해서 StringBuilder에 pop해주니 한번에 통과가되네요...

혹시 제가 놓친 부분이 있을까요? 

siyamaki   2년 전

10을 뒤집으면 01이 됩니다

gun0680   2년 전

siyamaki

감사합니다.

댓글을 작성하려면 로그인해야 합니다.