1.11 Learning Blog

Today I summarize a divide and conquer problem and a dp problem

Divide and conquer problem : sorting matrix search
Insert picture description here
ideas: reduce the size of the problem (reduction).
Set matrix[i][j] in the lower left corner of the matrix, which is the minimum value in the ith row and the maximum value in the jth column.
If target <matrix[i ][j] (less than the minimum value of the i-th row), then exclude the i-th row, let i-
if target> matrix[i][j] (greater than the maximum value of the j-th column), then exclude the j-th column and let j++
loop 2~3 until the target is found, or all ranks are excluded

Part of the code is as follows :


bool searchMatrix(int** matrix, int matrixSize, int matrixColSize, int target){
    
    
int i=matrixSize-1;
int k=matrixColSize;
int j=0;
for(;i>=0&&j<k;){
    
    
    if(target==matrix[i][j])return true;
    if(target<matrix[i][j])i--;
    else{
    
    j++;}
}
return false;
}

dp topic: the longest common subsequence
Insert picture description here
Insert picture description here
, here is a summary of the typical algorithm by a big guy, and labuladong’s algorithm speculation

借鉴题解中的分享,对于本题来说第一步要做的就是定义dp数组的含义,首先我们来看题目,
题目要求找出两个字符串的最长公共子序列,一般对于在两个字符串,
或者是能拆解成两个字符串的问题当中,我们都定义一个二维dp数组。
dp[i][j]:表示在字符串s[0...i]中和字符串s[0...j]中最长公共子序列的长度为dp[i][j]

Dp diagram of the sample
We will represent the first column as an empty string, for example: the length of the longest common substring between "" and "babcde" is 0.

Next, start looking for the state transition equation . This is the most critical step, and I think the most exciting step written by the boss. A common subsequence is required, and all letters in the common subsequence must come from s1 and s2. Then we can say the other way around, the letters in s1 and s2 are either in the common subsequence or not in the common subsequence.

s1[i] == s2[j], it means in, so the current result is equal to the result of the string that did not enter before + 1.
s1[i]!=s2[j] means that at least one of them is absent (or means ** not ** have at least one (or s1 [i] is not, or is not, or s_2 [j] $ is not, or not in)., the current result is equivalent to the previous results in a maximum that
took on It also means mathematical induction. Assuming we have calculated dp[i][j−1], how do we calculate dp[i][j] (that is, it is equivalent to a new character in the string). We can write directly based on the above

Part of the code is as follows:

int max(int a,int b){
    
    
    if(a<b)return b;
    else{
    
    return a;}
}
int longestCommonSubsequence(char * text1, char * text2){
    
    
int n=strlen(text1),m=strlen(text2);
int dp[n+1][m+1];
for(int i=0;i<n+1;i++)dp[i][0]=0;
for(int i=0;i<m+1;i++)dp[0][i]=0;
for(int i=1;i<n+1;i++){
    
    
    for(int j=1;j<m+1;j++){
    
    
        if(text1[i-1]==text2[j-1]){
    
    
            dp[i][j]=dp[i-1][j-1]+1;
        }
        else{
    
    
            dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }
}
return dp[n][m];
}

Today I came into contact with the idea of ​​divide and conquer and consolidated the dp. I think it is necessary to share with you the above algorithm summary of the boss. That is what I saw while watching the solution of the boss problem. Everyone makes progress together!

Guess you like

Origin blog.csdn.net/weixin_47529865/article/details/112482589