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. 深入理解java虚拟机(五)垃圾收集器

    垃圾收集器 垃圾收集器是垃圾收集算法的具体实现.Java规范对垃圾收集器的实现没有做任何规定,因此不同的虚拟机提供的垃圾收集器可能有很大差异.HotSpot虚拟机1.7版本使用了多种收集器.如下图. ...

  2. 上下文——webApplicationContext 与servletContext

    1.WebApplicationContext的研究 ApplicationContext是spring的核心,Context通常解释为上下文环境,用“容器”来表述更容易理解一些,Applicatio ...

  3. Rabbimq必备基础之对高级消息队列协议AMQP分析及Rabbitmq本质介绍

    MQ的一个产品... [消息队列] 1. MSMQ windows自带的一个服务... [petshop],message存放在文件系统中. 最原始的消息队列... [集群,消息确认,内存化,高可用, ...

  4. js 操作cookie cookie路径问题

    这里主要不是讲这个方法,js写cookie这种代码网上一抓一把,在使用的时候遇到一点问题,就是写的cookie 是有路径问题的,在user目录下可以使用跳转到另外一个目录下cookie,经过比较coo ...

  5. [Erlang05]gen_server怎么去写eunit?

    Prework: 怎样写一个基本的Eunit? Doc. 1. 加入头文件:声明此模块以”_test”结尾的函数都是测试用,并在编译时自动在这个模块里加入test()函数(当然这个可以用宏来控制) - ...

  6. hadoop2.2.0编译、安装和测试

    搭建环境:单机64位CentOS6.5 .jdk1.6.0_45.Hadoop2.2.0 1.准备编译环境 从http://www.apache.org/dyn/closer.cgi/hadoop/c ...

  7. Ocelot

    Ocelot——初识基于.Net Core的API网关 Ocelot API网关的实现剖析 微服务网关Ocelot API网关Ocelot 使用Polly 处理部分失败问题 谈谈微服务中的 API 网 ...

  8. nexus3

    Maven 介绍 Apache Maven 是一个创新的软件项目管理和综合工具. Maven 提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和 ...

  9. CSS3水平翻转样式和background-size兼容问题

    一.水平翻转和垂直翻转:第一种:随着现代浏览器对CSS3的支持愈发完善,对于实现各个浏览器兼容的元素的水平翻转或是垂直翻转效果也就成为了可能.相关的CSS代码如下: /*水平翻转*/ .flipx { ...

  10. “全栈2019”Java第十八章:一元运算符

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...