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. HDU 3366 Passage (概率DP)

    题意:T组测试数据,一个人困在了城堡中,有n个通道,m百万money ,每个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i], 遇到士兵得给1百万money,否则会被杀掉,还有 1-p ...

  2. Introduction mybatis

    项目地址 https://github.com/mybatis/mybatis-3 英文官网 http://mybatis.github.io/mybatis-3/ 中文官网 http://mybat ...

  3. 练习题。对DOM中document的深刻理解巩固

    //window.onload = modTwo;     1.点击单元格内容  弹窗promrt接收值   将接受的值提换单元格内容    2.点击单元格  出现2个按钮 加粗 字体颜色标红     ...

  4. linux安装x264 ffmpeg

    1. 安装yasm 2. 安装x264 3. 安装ffmpeg 安装网上很多例子,以下是我主要参考的两篇博客: http://www.cnblogs.com/lidabo/p/3987378.html ...

  5. .net 有参属性 index (索引)

    public class IndexTempte { public ArrayList nameList = new ArrayList(); public string this[int index ...

  6. mybatis--mapper配置总结

    mapper介绍 mapper使用规则:按业务划分,一个业务模块相关的sql均定义在一个mapper文件 mapper的xml格式: doctype: <!DOCTYPE mapper PUBL ...

  7. CancellationTokenSource 取消任务

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

  8. centos7 守护进程

    ASP.NET Core应用程序发布linux在shell中运行是正常的.可一但shell关闭网站也就关闭了,所以要配置守护进程, 用的是Supervisor,本文主要记录配置的过程和过程遇到的问题 ...

  9. nowcoder(牛客网)普及组模拟赛第一场 解题报告

    蒟蒻我可能考了一场假试 T1 绩点 这题没什么好说的,应该是只要会语言的就会做. T2 巨大的棋盘 一个模拟题吧qwq,但是要注意取模的时候先加上n或者m再取模,要不然会错的. #include< ...

  10. 【qbxt五一】day2

    简单数据结构 入门题: 在初学OI的时候,总会遇到这么一道题. 给出N次操作,每次加入一个数,或者询问当前所有数的最大值. 维护一个最大值Max,每次加入和最大值进行比较. 时间复杂度O(N). 给出 ...