[] Dynamic programming to find the longest common subsequence

1. What is the longest common subsequence (LCS)
defines: a number of columns S, respectively, if the sequence of one or more other known number of columns, and this condition is all the longest match, said to be known S longest common subsequence number sequence;
Note that: the sub-sequences need not occupy consecutive positions in the original sequence.
EGE:
the X-sequence: ABCBDAB
the y-sequence: BDCABA
their longest common subsequence is: BCAB, BDAB, BCBA
then we find out how this common subsequence?
(1) exhaustive
one by one to find, easy to understand, but cumbersome solution, the time taken great complexity, is O (n * 2 ^ m) , is generally not recommended.
(2) Simplification:. A search length of the longest common subsequence
b find sequences.
As:
Here Insert Picture Description
Here Insert Picture Description
Method 1] recursive)

package 查找最长公共子序列;

public class 递归 {
	public static int lcs(char[] a, char[] b, int i, int j) {
		if (i == 0 || j == 0) {
			return 0;
		} else if (a[i] == b[j]) {
			return lcs(a, b, i - 1, j - 1) + 1;
		} else {
			return max(lcs(a, b, i - 1, j), lcs(a, b, i, j - 1));
		}
	}

	private static int max(int x, int y) {
		if (x > y) {
			return y;
		} else {
			return y;
		}
	}

	public static void main(String[] args) {
		String s1 = "ABCBDAB";
		String s2 = "BDCABA";
		System.out.println(lcs(s1.toCharArray(), s2.toCharArray(), s1.length() - 1, s2.length() - 1));

	}

}

The results of such execution is three, obviously not, because at the time of judgment 0th element is empty, but the array element 0 is actually uploaded A, is not empty, it returns 0 count less likely, so it is necessary expansion of the two sub-sequences, that is to find ways in front of the two sub-sequences plus 0
solution: two sub-sequences into a new array of characters with a 0 character. Such as:

package 查找最长公共子序列;

public class 递归 {
	public static int lcs(char[] a, char[] b, int i, int j) {
		if (i == 0 || j == 0) {
			return 0;
		} else if (a[i] == b[j]) {
			return lcs(a, b, i - 1, j - 1) + 1;
		} else {
			return max(lcs(a, b, i - 1, j), lcs(a, b, i, j - 1));
		}
	}
	private static int max(int x, int y) {
		if (x > y) {
			return x;
		} else {
			return y;
		}
	}
	public static void main(String[] args) {
		String s1 = "ABCBDAB";
		char[] c1 = new char[s1.length() + 1];
		char[] t1 = s1.toCharArray();
		c1[0] = (char)0;
		for(int i = 0;i < t1.length;i++) {
			c1[i + 1] = t1[i];
		}
		String s2 = "BDCABA";
		char[] c2 = new char[s2.length() + 1];
		char[] t2 = s2.toCharArray();
		c2[0] = (char)0;
		for(int i = 0;i < t2.length;i++) {
			c2[i + 1] = t2[i];
		}
		System.out.println(lcs(c1, c2, c1.length - 1, c2.length - 1));
	}
}

4. Thus the output result of
Method 2 :( memorandum)

package 查找最长公共子序列;

public class 递归 {
	public static int lcs(char[] a, char[] b, int i, int j, int[][] bak) {
		//如果bak[i][j] != -1,说明不是初值,已经计算过了,直接返回备忘录里面的值 
		if (bak[i][j] != -1) {
			return bak[i][j];
		}
		/*
		 * 否则将数值存进备忘录里面
		 */
		if (i == 0 || j == 0) {
			bak[i][j] = 0;
		} else if (a[i] == b[j]) {
			bak[i][j] = lcs(a, b, i - 1, j - 1, bak) + 1;
		} else {
			bak[i][j] = max(lcs(a, b, i - 1, j, bak), lcs(a, b, i, j - 1, bak));
		}
		return bak[i][j];
	}

	private static int max(int x, int y) {
		if (x > y) {
			return x;
		} else {
			return y;
		}
	}

	public static void main(String[] args) {
		String s1 = "ABCBDAB";
		char[] c1 = new char[s1.length() + 1];
		char[] t1 = s1.toCharArray();
		c1[0] = (char) 0;
		for (int i = 0; i < t1.length; i++) {
			c1[i + 1] = t1[i];
		}
		String s2 = "BDCABA";
		char[] c2 = new char[s2.length() + 1];
		char[] t2 = s2.toCharArray();
		c2[0] = (char) 0;
		for (int i = 0; i < t2.length; i++) {
			c2[i + 1] = t2[i];
		}
		int[][] bak = new int[c1.length][c2.length];
		//将备忘录里面的值初始化为 -1;
		for (int i = 0; i < c1.length; i++) {
			for (int j = 0; j < c2.length; j++) {
				bak[i][j] = -1;
			}
		}
		System.out.println(lcs(c1, c2, c1.length - 1, c2.length - 1, bak));
	}
}

Law 3: self bottom improved)
Here Insert Picture Description

package 查找最长公共子序列;

import java.util.Scanner;

public class 自底向上 {
	public static int lcs(char[] a, char[] b, int i, int j, int[][] bak) {
		/*
		 * i和j 都是索引
		 */
		for (int ii = 0; ii <= i; ii++) {
			for (int jj = 0; jj <= j; jj++) {
				if (ii == 0 || jj == 0) {
					bak[ii][jj] = 0;
				} else if (a[ii] == b[jj]) {
					bak[ii][jj] = bak[ii - 1][jj - 1] + 1;// 前面已经计算过了
				} else {
					bak[ii][jj] = max(bak[ii - 1][jj], bak[ii][jj - 1]);
				}
			}
		}
		return bak[i][j];// 返回最大值的位置
	}

	private static int max(int x, int y) {
		if (x > y) {
			return x;
		} else {
			return y;
		}
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		String s1 = s.nextLine();
		char[] c1 = new char[s1.length() + 1];
		char[] t1 = s1.toCharArray();
		c1[0] = (char) 0;
		for (int i = 0; i < t1.length; i++) {
			c1[i + 1] = t1[i];
		}
		String s2 = s.nextLine();
		char[] c2 = new char[s2.length() + 1];
		char[] t2 = s2.toCharArray();
		c2[0] = (char) 0;
		for (int i = 0; i < t2.length; i++) {
			c2[i + 1] = t2[i];
		}
		int[][] bak = new int[c1.length][c2.length];
		System.out.println(lcs(c1, c2, c1.length - 1, c2.length - 1, bak));
	}

}

Guess you like

Origin blog.csdn.net/weixin_44084434/article/details/90266466
Recommended