Home BOJ. LCS 2 (9252)
Post
Cancel

BOJ. LCS 2 (9252)

[Link] https://www.acmicpc.net/problem/9252


DP solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static int max = 0, maxIdx = 0;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String line1 = br.readLine(), line2 = br.readLine();
		int l1 = line1.length(), l2 = line2.length();
		int[][] dp = new int[l1][l2]; // dp[i][j]: line1 0 ~ i line 0 ~ j

		for(int i = 0; i < l1; i++) {
			for(int j = 0; j < l2; j++) {
				if(line1.charAt(i) == line2.charAt(j)) {
					if(i >= 1 && j >= 1) dp[i][j] = dp[i-1][j-1] + 1;
					else dp[i][j] = 1;
				} else {
					int v1 = i > 0 ? dp[i - 1][j] : 0;
					int v2 = j > 0 ? dp[i][j - 1] : 0;
					dp[i][j] = Math.max(v1, v2);
				}
			}
		}
		println(dp[l1-1][l2-1]);
		Stack<Integer> stack = new Stack<>();
		int store = dp[l1-1][l2-1];
		int ii = l1-1, jj = l2-1;
		while(ii >= 0 && jj >=0) {
			if(dp[ii][jj] == 0) break;
			if(line1.charAt(ii) == line2.charAt(jj)) {
				sb.insert(0, line1.charAt(ii));
				ii--;
				jj--;
				continue;
			}
			int upVal = ii > 0 ? dp[ii-1][jj] : 0;
			int preVal = jj > 0 ? dp[ii][jj-1] : 0;
			int max = 0;
			boolean goUp = true;
			if(upVal > preVal) max = upVal;
			else {
				max = preVal;
				goUp = false;
			}
			if(goUp) ii--;
			else jj--;
		}
		print(sb);
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}


Time Limit Exceed(Recursive)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static int max = 0, maxIdx = 0;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		// String[] line = getLine();
		String line1 = br.readLine(), line2 = br.readLine();
		String[][] dp = new String[line1.length()][line2.length()];
		for(String[] tmp : dp) Arrays.fill(tmp, "");
		String result = lcs(line1, line2, 0, 0, dp);
		if(result.length() == 0) sb.append("0");
		else sb.append(result.length()).append("\n").append(result);
		print(sb);
	}

	static String lcs(String line1, String line2, int idx1, int idx2, String[][] dp) {
		if(idx1 >= line1.length() || idx2 >= line2.length()) return "";
		if(dp[idx1][idx2].length() != 0) return dp[idx1][idx2];
		if(line1.charAt(idx1) == line2.charAt(idx2))
			return dp[idx1][idx2] = line1.charAt(idx1) + lcs(line1, line2, idx1 + 1, idx2 + 1, dp);
		String s1 = lcs(line1, line2, idx1 + 1, idx2, dp), s2 = lcs(line1, line2, idx1, idx2 + 1, dp);
		return dp[idx1][idx2] = s1.length() > s2.length() ? s1 : s2;
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.