A palindrome is a nonempty string over some alphabet that reads the same forward
and backward. Examples of palindromes are all strings of length 1, civic,
racecar, and aibohphobia (fear of palindromes).
Give an efficient algorithm to find the longest palindrome that is a subsequence
of a given input string. For example, given the input character, your algorithm
should return carac. What is the running time of your algorithm?

struct Palindrome {
std::string strPalindrome;
std::string strFront;
std::string strEnd;
}; using VectorOfVectorOfIntermediate = std::vector<std::vector<Palindrome>>; std::string findLongestPalindrome(const std::string &strInput) {
VectorOfVectorOfIntermediate vecVecIntermediate = VectorOfVectorOfIntermediate(strInput.size(), std::vector<Palindrome>(strInput.size() + ));
for ( int i = ; i < strInput.size(); ++ i ) {
vecVecIntermediate[i][].strPalindrome = strInput[i];
vecVecIntermediate[i][].strFront = "";
vecVecIntermediate[i][].strEnd = "";
} for ( int i = ; i < strInput.size() - ; ++ i ) {
if ( strInput[i] == strInput[i + ]) {
vecVecIntermediate[i][].strPalindrome = strInput.substr(i, );
vecVecIntermediate[i][].strFront = "";
vecVecIntermediate[i][].strEnd = "";
}else {
vecVecIntermediate[i][].strPalindrome = strInput[i];
vecVecIntermediate[i][].strEnd = strInput[i+];
}
} for ( int L = ; L <= strInput.size(); L += ) {
for ( int n = ; n <= strInput.size() - L; ++ n ) {
size_t findPos; Palindrome p2 = vecVecIntermediate[n][L-];
p2.strEnd.push_back ( strInput[n + L - ] );
findPos = p2.strFront.find_first_of ( p2.strEnd );
if ( findPos != std::string::npos) {
auto charFind = p2.strFront[findPos];
p2.strPalindrome.insert ( p2.strPalindrome.begin(), charFind );
p2.strPalindrome.push_back ( charFind );
p2.strFront = p2.strFront.substr ( , findPos );
findPos = p2.strEnd.find ( charFind );
p2.strEnd = p2.strEnd.substr ( findPos + );
} Palindrome p3 = vecVecIntermediate[n+][L-];
p3.strFront.insert ( p3.strFront.begin(), strInput[n] );
findPos = p3.strFront.find_first_of ( p3.strEnd );
if ( findPos != std::string::npos) {
auto charFind = p3.strFront[findPos];
p3.strPalindrome.insert ( p3.strPalindrome.begin(), charFind );
p3.strPalindrome.push_back ( charFind );
p3.strFront = p3.strFront.substr ( , findPos );
findPos = p3.strEnd.find ( charFind );
p3.strEnd = p3.strEnd.substr ( findPos + );
} std::vector<Palindrome> vecP{p2, p3};
int nMaxIndex = ;
for ( int index = ; index < vecP.size(); ++ index ) {
if ( vecP[index].strPalindrome.length() > vecP[nMaxIndex].strPalindrome.length() ) {
nMaxIndex = index;
}
}
vecVecIntermediate[n][L] = vecP[nMaxIndex];
}
}
return vecVecIntermediate[][strInput.size()].strPalindrome;
} void Test_findLongestPalindrome()
{
{
std::string strTestCase("a");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("aa");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("ab");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("abbac");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("abcdefghijkjhgfed");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("character");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
} {
std::string strTestCase("GEEKS FOR GEEKS");
auto strPalindrome = findLongestPalindrome ( strTestCase );
std::cout << "Test case " << strTestCase << ", result " << strPalindrome << std::endl;
}
}

Longest palindrome subsequence的更多相关文章

  1. Uva 11151 - Longest Palindrome

    A palindrome is a string that reads the same from the left as it does from the right. For example, I ...

  2. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  8. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  9. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

随机推荐

  1. tomcat 无法clean 的bug

    如果你打开类似这种的文件夹了,那恭喜你,你无法正常clean E:\e\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0 请关 ...

  2. 大型Unity手游《英雄之刃-最后之战》源码分析

    英雄之刃之最后一战是国内首款原创精品MOBA手游,是一款由前暴雪文案亲自操刀世界观,日韩专业团队打造美术场景,新加坡团队精心制作战斗音乐的旷世之作! 超快速的匹配对战.默契的团队协作给你带来意犹未尽的 ...

  3. PreparedStatementSQLException

    目录 文章背景 目录 问题分析 问题解决 说明 参考文章 版本记录 layout: default title: PreparedStatementSQLException category: [Te ...

  4. servlet及xml文件处理流程

    启动项目----会找到web.xml文件---跳转到默认jsp----页面重定向----转到xml.文件下 通过<servlet-mapping>映射找到<servlet>标签 ...

  5. 关于Javascript 那些事

    Javascript是一种由Netscape的LiveScript发展而来的原型化继承的面向对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为 ...

  6. 学习python5面向

    类有一个名为 __init__() 的特殊方法(构造方法),该方法在类实例化时会自动调用 面向过程:根据业务逻辑从上到下写代码 面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减 ...

  7. 关于HTTP协议传输与接收数据的相关内容

    第一篇: HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/28/2612910.html 第二篇: 深入浅出U ...

  8. border使用小技巧

    border-style 分类 dashed虚线类型 dotted 点线类型 double 双线类型 双线型量根实线的宽度和中间空白区域的间距有一定规律: 可以利用这个规律画出一些特殊的图案 代码如下 ...

  9. BitArray简单例子

    using System; using System.Collections; using System.Text; namespace TestConsole { class Program { s ...

  10. CancellationTokenSource 取消任务

    using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp1 { class Pr ...