Longest Palindromic Substring [leetcode] O (n ^ 2) DP and O (n) algorithms, longestsubstring

Source: Internet
Author: User

Longest Palindromic Substring [leetcode] O (n ^ 2) DP and O (n) algorithms, longestsubstring

Two methods are implemented. One is DP, which is made of loops. recursion is easier.

string longestPalindrome(string s) {        int n = s.size();        bool dp[1001][1001];        int maxl = 1;        int maxs = 0;        for (int i = n - 1; i >= 0; i--)        {            dp[i][i] = true;            for (int j = i + 1; j < n; j++)            {                if (s[i] == s[j] && (j == i + 1 || dp[i + 1][j - 1]))                {                    dp[i][j] = true;                    if (maxl < (j - i + 1))                    {                        maxl = (j - i + 1);                        maxs = i;                    }                }                else                    dp[i][j] = false;            }        }        return s.substr(maxs, maxl);    }


Another method is the well-known O (n) method.

string longestPalindrome(string s) {        int n = s.size();        int newn = n * 2 + 1;        int p[2000];        char * news = new char[newn];        for (int i = 0; i < n; i++)        {            news[2 * i] = '*';            news[2 * i + 1] = s[i];        }        news[2 * n] = '*';        //s:abac        //news:*a*b*a*c*         //P:   121412121        int mx = 0;        int mi = 0;        int maxLen = 0;        int maxIndex = 0;        for (int i = 0; i < newn; i++)        {            if (mx > i)                p[i] = min(p[2 * mi - i], mx - i);            else                p[i] = 1;            for (;i - p[i] >= 0 && i + p[i] < newn && news[i + p[i]] == news[i - p[i]];p[i]++);            if (p[i] + i > mx)            {                mx = p[i] + i;                mi = i;            }            if (maxLen < p[i] - 1)            {                maxLen = p[i] - 1;                maxIndex = i;            }        }        string returnVal = s.substr((maxIndex - maxLen) / 2, maxLen);        delete [] news;                return returnVal;    }







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.