Longest Palindromic Substring

原创 2015年07月07日 14:58:05

暴力解法:


#include<iostream>
#include<string>
using namespace std;
string expand(string a,int l,int r)
{

    while(l>=0&&r<a.size())
    {

        if(a[l]==a[r])
        {
            l--;
            r++;
        }
        else
            break;
    }

    //cout<<a.substr(l+1,r-l-1)<<endl;
    return a.substr(l+1,r-l-1);
}

string longstr(string a)
{
    if(a.size()==0)
        return " ";
    if(a.size()==1)
        return a;
    int longeststr=1;
    string str1,str2,str=a.substr(0,1); 

    for(int i=0;i<a.size()-1;i++)
    {
         str1=expand(a,i,i);
         if(str1.size()>longeststr)
        {
            longeststr=str1.size();
            str=str1;

        }
        str2=expand(a,i,i+1);
        if(str2.size()>longeststr)
        {
            longeststr=str1.size();
            str=str2;
        }
    }
    return str;
}

void main()
{
    string a="abdfdbsdf";
    a=longstr(a);
    cout<<a;

}

Manacher’s Algorithm解法:


#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Solution
{
public: string para(string a)
    {
        string result;
        if(a.length()<2)
            return a;
        vector<int>p(a.length()*2+1,0);
        result.push_back('#');
        for(int i=0;i<a.length();i++)
        {
            result.push_back(a[i]);
            result.push_back('#');
        }

        int id=0;
        int max=0;
        for(int i=0;i<2*a.length()+1;i++)
        {
            if(max>i)
            {
                if(p[2*id-i]>max-i)
                    p[i]=max-i;
                else
                    p[i]=p[2*id-i];

            }
            else
                p[i]=0;
            while(i-p[i]-1>=0&&i+p[i]+1<2*a.length()+1&&result[i+p[i]+1]==result[i-p[i]-1])
                p[i]++;
            if(max<p[i]+i)
            {               
                max=p[i]+i;
                id=i;
            }
        }
        int maxlen=0;
        int maxid=0;
        for(int i=0;i<result.length();i++)
        {
            if(maxlen<p[i])
            {
                maxlen=p[i];
                maxid=i;
            }
        }
        return a.substr((maxid-p[maxid])/2,p[maxid]);
    }
};

void main()
{
    Solution solution;
    string a("1211213214111");
    cout<<solution.para(a)<<endl;
}

相关文章推荐

leetcode之路005 Longest Palindromic Substring

#include "stdafx.h" #include #include #include #include using namespace std; class Solution { pu...

leetcode 5: Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum len...

leetcode 214. Shortest Palindrome leetcode 5. Longest Palindromic Substring回文串相关

leetcode 214. Shortest Palindrome 一看这道题第一反应就是先找出以str[0]为起点的最长的回文子串,然后,将这个回文子串后面的子串反转再拼接到前面。 然后第一步,看看...

[LeetCode]5 Longest Palindromic Substring(C++,Python实现)

LeetCode OJ的第五题,如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述...

leetcode-5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt...
  • fao9001
  • fao9001
  • 2017年05月03日 09:08
  • 114

Longest Palindromic Substring(leetcode medium algorithm problem)

本题是LeetCode上的一道中等难度题。 题目是寻找字符串中最长的回文串——原题链接Input: “babad”Output: “bab”Note: “aba” is also a valid a...

LeetCode#5* Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt...

(Leetcode)5. Longest Palindromic Substring(medium)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt...

Longest Palindromic Substring(leetcode5)

题目描述: Given a string s, find the longest palindromic substring in s. You may assume that the maxim...

LeetCode OJ:Longest Palindromic Substring

Longest Palindromic Substring   Given a string S, find the longest palindromic substring in S. Yo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Longest Palindromic Substring
举报原因:
原因补充:

(最多只允许输入30个字)