Dynamic Programming Longest Common Character Subsequence

question

Find the longest common subsequence of a two-character sequence

answer

Speaking of dynamic programming, we have to talk about his recursive formula. Often this formula is not easy to see at a glance, we need to find a rule, and this is what we need to overcome. My idea is to make a diagram first. Look for patterns in the process of continuous drawing.

draw

The process of drawing a picture is actually a process of interpreting the subject. The point is how to set the abscissa and ordinate. The problem with subsequences, of course, is letters. For example the following abc and bcd

| - | a | b | c |
|:-------:|:-------: | :-------:|
| b | 0 | 1 |1|
|c|-|-|-|
|d|-|-|-|

Look at the first line first. In fact, it represents the subsequence result of b in a, ab, and abc. In the subsequent process of x, the previous results need to be compared, and the largest one is taken. Now consider the second line

| - | a | b | c |
|:-------:|:-------: | :-------:|
| b | 0 | 1 |1|
| c|0|1|2|
|d|-|-|-|
The second line is different. Represents the subsequence of bc in a, ab, and abc. When filling in ac, it is good to consider the matching between the value of b and c itself. ab is 0, and c does not match a, so the result can only be 0. The situation of bc needs to consider the matching situation of a and bc, the matching situation of b and ab, and the matching situation of c itself. In contrast, the largest is 1 (the contents of the bb and ac boxes are compared), and the situation of cc is the same, but the comparison is the same. In the same situation, you need to look at the situation without c, that is, ab and b. In comparison, if one more character hits, the results of one less case need to be accumulated. Compare that to 2. The third line is actually similar to the second line.
| - | a | b | c |
|:-------:|:-------: | :-------:|
| b | 0 | 1 |1|
| c|0|1|2|
|d|0|1|2|
d did not hit any strings. So until the cd, the result is still 2. String comparison is complete.

Code Implementation Reference

    public static int checkString(String a, String b) {

        int length = 0;
        if (a == null || b == null || a.isEmpty() || b.isEmpty()) {
            return length;
        }
        char[] aChars = a.toCharArray();
        char[] bChars = b.toCharArray();
        int[][] dp = new int[aChars.length][bChars.length];

        for (int j = 0; j < aChars.length; j++) {
            if (bChars[0] == aChars[j]) {
                dp[j][0] = 1;
            }
        }
        for (int i = 1; i < bChars.length; i++) {
            for (int j = 0; j < aChars.length; j++) {
                if (bChars[i] == aChars[j]) {
                    if (j > 0) {
                        dp[j][i] = dp[j - 1][i - 1] + 1;
                    } else {
                        dp[j][i] = Math.max(dp[j][i - 1], 1);
                    }
                } else {
                    if (j > 0) {
                        dp[j][i] = Math.max(dp[j - 1][i], dp[j][i - 1]);
                    } else {
                        dp[j][i] = Math.max(dp[j][i - 1], 0);
                    }
                }
            }
        }
        return dp[aChars.length - 1][bChars.length - 1];
    }

Summarize

Dynamic programming has a recursive formula. But this formula can be written out without needing to be displayed, and it can also be done by looking for rules in the process of drawing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324480564&siteId=291194637