最长回文子串
2013-10-06 21:11
211 查看
/*given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.*/ #include <string> #include <iostream> class Solution { public: std::string longestPalindrom(std::string s) { if(s.size()==0) return s; int l=-1,r=-1; for(int i=0;i<s.size();++i) { int ll=i-1; int rr=i+1; int lll=i; int rrr=i+1; while(ll>=0 && rr<s.size() && s[ll]==s[rr]) { --ll; ++rr; } while(lll>=0 && rrr<s.size() && s[lll]==s[rrr]) { --lll; ++rrr; } if(rr-ll>r-l) { r=rr; l=ll; } if(rrr-lll>r-l) { r=rrr; l=lll; } } if(r-l-1>0) return s.substr(l+1,r-l-1); return std::string(); } }; int main(int argc,char* argv[]) { std::string str("abadcefabcdcba"); Solution s; std::cout<<s.longestPalindrom(str)<<std::endl; return 0; }
相关文章推荐
- LeetCode 5 最长回文子串 Manacher算法
- hihocoder 1032 最长回文子串
- LeetCode 5 最长回文子串 Manacher线性算法
- 51nod--1089 最长回文子串V2(Manacher 算法)
- 最长回文子串 leetcode
- 51Nod - 1089 最长回文子串 V2(Manacher算法)
- 51nod1088---最长回文子串(51nod基础:模拟)
- 最长公共子串、最长公共子序列、最长回文子串、最长回文子序列、回文子串个数
- 最长回文子串
- 最长回文子串(Manacher算法模板题)&&对称字符串问题
- POJ 3974 Palindrome(最长回文子串)
- Manacher算法: O(n)时间求字符串的最长回文子串
- HDU 3068 最长回文子串
- acdream 1722(最长回文子串)
- 求解最长回文子串
- 最长回文子串--轻松理解Manacher算法
- 最长回文子串
- 对于一个字符串,请设计一个高效算法,计算其中最长回文子串的长度。
- 2014百度校园招聘笔试——求一个字符串的最长回文子串
- HDU 3068 最长回文子串(manacher算法)