[Leetcode]-5-The longest palindrome substring

[Method 1: Dynamic programming]

Step 1: Determine the status

If a substring is a palindrome, adding the same characters at the beginning and end of it is still a palindrome.

The last step: when different characters are added to both ends of the palindrome, the length of the palindrome will not increase.

Sub-problem: Assuming that s[i][j] represents a substring from i to j, it is necessary to check whether s[i+1][j-1] is a palindrome before judging whether s[i][j] is a palindrome Palindrome

Step 2: Transfer equation

Step 3: Initial conditions and boundary values

Initial condition: The value of a single string must be a palindrome, so f[i][i]=true on the diagonal

It is equivalent to j-i+1<4, which is equivalent to when there is no character at the end or only one character is removed at the end, no need to continue to check whether the substring is a palindrome, directly set f[i][j]=true

The fourth step: the order of operations

When calculating f[i][j], refer to the value of f[i+1][j-1], and f[i+1][j-1] is at the bottom left of f[i][j], Therefore, row-first traversal is not possible, only column-first. This will ensure that when calculating f[i][j], the lower left corner has been calculated.

  • Closing characters are not equal
    • Not palindrome
  • Ending characters are equal
    • After removing the ending characters, there is only one character or 0 characters, which can be directly judged as a palindrome.
    • The length is enough to further determine whether the substring is a palindrome
  string longestPalindrome(string s) {
        int n=s.length();
        if(n<2)return s;

        vector<vector<bool>> f(n, vector<bool>(n,false));
        int begin=0;
        int maxlen=1;//回文串的长度至少是1
        for(int i=0;i<n;i++)f[i][i]=true;//对角线初始化为true
       
        for(int j=1;j<n;j++){//列优先,所以从j开始遍历,f[0][0]和f[n-1][n-1]已经被初始化了
            for(int i=0;i<j;i++){
                if(s[i]!=s[j])f[i][j]=false;//如果收尾字符不相等,直接判断不是回文串
                else {//如果收尾字符相等
                    if(j-i<3)f[i][j]=true;//去掉收尾字符只剩下1个字符或者0个字符,直接判断是回文串
                    else f[i][j]=f[i+1][j-1];
                }

                if(f[i][j]&&j-i+1>maxlen)
                {
                    maxlen=j-i+1;
                    begin=i;
                }
            }
        }
        return s.substr(begin,maxlen);

    }

[Method 2: Center diffusion method]

  • Look to the left for the same character as the current position until it encounters an inequality.
  • Look to the right for the same character as the current position until it encounters an inequality.
  • Finally, it spreads in both directions until left and right are not equal.
  • Note: s.substr (start point, length)
 string longestPalindrome(string s) {
        if(s.length()<2)return s;
        int left=0,right=0;
        int len=1,maxlen=0,maxStart = 0;

        for(int i=0;i<s.length();i++)
        {
            left=i-1;
            right=i+1;
            while(left>=0&&s[left]==s[i]){
                left--;
                len++;
            }
            while(right<s.length()&&s[right]==s[i]){
                right++;
                len++;
            }
            while(left>=0&&right<s.length()&&s[left]==s[right]){
                left--;
                right++;
                len+=2;
            }
            if(len>maxlen){
                maxlen=len;
                maxStart=left;
            }
            len=1;
        }
        return s.substr(maxStart+1,maxlen);
    }

 

Guess you like

Origin blog.csdn.net/qq_39328436/article/details/112685841