[Niuke quiz] The longest non-repeating subarray, the longest common substring, the longest palindrome substring

  The longest non-repeating subarray_NiukeTiba_Niuke.com

 Double pointer:

Traverse backward starting from the first element of the array. The two pointers `start` and `end` both start from the beginning of the array. The `end` pointer will traverse the entire array one by one, while the `start` pointer will only move when it is necessary to skip repeated elements.

For example, for the array `[1,2,3,4,1,5,6,7,8,1]`, start=0` and end=0` at the beginning. When `end` traverses to the second `1` (i.e. `arr[4]`), the `start` pointer will skip the first `1` and move to `arr[1]` (i.e. element `2 `) place.

In this way, there are no repeated elements in the subarray in the `[start, end]` interval. We only need to record the length of this interval and compare it with the maximum length found so far.

The `unordered_map` of `lastPos` is used to quickly find the last occurrence of an element so that we can quickly update the `start` pointer.

int maxLength(vector<int>& arr) {
        if (arr.empty()) return 0;

        int maxLen = 0;
        unordered_map<int, int> lastPos;
        int start = 0;

        for (int end = 0; end < arr.size(); ++end) {
            // 更新start指针位置
            if (lastPos.find(arr[end]) != lastPos.end() && lastPos[arr[end]] >= start) {
                start = lastPos[arr[end]] + 1;
            }

            // 更新元素最后出现的位置
            lastPos[arr[end]] = end;

            // 更新最大长度
            maxLen = max(maxLen, end - start + 1);
        }

        return maxLen;
    }

The longest public substring_NiukeTiba_Niuke.com

 Just record the subscript, and then use the substr function , str1.substr(maxIndex - maxLen, maxLen);

`substr` is a member function commonly used in the C++ `std::string` class to extract a substring from a string. This function takes two parameters:

1. `pos` (starting position): From which position to start extracting the substring. Positions start counting from 0.
2. `len` (length): The length of the substring to be extracted.

    string LCS(string str1, string str2) {
        // write code here
        int maxLen = 0;
        int maxIndex;
        vector<vector<int>> dp(str1.size() + 1, vector<int> (str2.size() + 1, 0));
        for (int i = 1; i <= str1.size(); i++) {
            for (int j = 1; j <= str2.size(); j++) {
                if (str1[i-1] == str2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1; 
                }
                
                if (maxLen < dp[i][j]) {
                        maxIndex = i;
                        maxLen = dp[i][j];
                }
            }
        }
        string res = str1.substr(maxIndex - maxLen, maxLen);

        return res;
    }

The Longest Palindrome Substring_Niuke Topic_Niuke.com

Note that it is different from the longest palindrome subsequence. It is similar to finding the total number of palindrome subsequences . Use bool type dp. Remember to update maxLen=max(maxLen, j - i +1) when dp[i][j]=true. , in addition i starts from size()-1, j starts from i 


    int getLongestPalindrome(string A) {
        // write code here
        vector<vector<bool>> dp(A.size(), vector<bool>(A.size(), false));
        int maxLen = 1;
        for (int i = A.size() - 1; i >= 0; i--) {
            for (int j = i; j < A.size(); j++) {
                if (A[i] == A[j]) {
                    if ((j - i) <= 1) {
                        dp[i][j] = true;
                    } else if (dp[i + 1][j - 1] == true) {
                        if (A[i] == A[j]) {
                            dp[i][j] = true;
                        }
                    }
                    if (dp[i][j]) maxLen = max(maxLen, j - i + 1);
                }
            }
        }
        return maxLen;
    }

Guess you like

Origin blog.csdn.net/weixin_43785314/article/details/132677357