Longest palindrome subsequence
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的更多相关文章
- 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 ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
随机推荐
- javafx 继承Application打开
前段时间需要用到javafx的Application来写一些图形界面之类的东西,但是run了之后eclipese不会去运行它,很纳闷,然后看了一下run as发现是没有main入口 其实加上一个mai ...
- spring Mybatis集成
pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...
- gulp-usemin 插件使用
关于什么是gulp,它和grunt有什么区别等问题,这里不做任何介绍.本文主要介绍如何使用gulp-usemin这款插件,同时也会简单介绍本文中用到的一些插件. 什么是gulp-usemin 用来将H ...
- MVC4 Model ValueProvider
1. NameValueCollectionValueProvider: ValueProvider 的数据容器一般具有类似字典的结构.NameValueCollection 表示一种 key 和va ...
- [转]java设计模式
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- Djangorestframework编写post接口
前提:已经有Django环境 一.安装 pip install djangorestframework 二.编写视图 import json from django.shortcuts import ...
- django 快捷代码提示
1.右键项目,Mark Directory As Source Root 2.settings配置 3.import包时可忽略app名了
- python3.7.0安装
如何安装Python的操作步骤: 1.第一步先去python的官方网站下载python的安装包 地址:https://www.python.org/downloads/ 根据自己的系统选择对应的安装包 ...
- JavaScript作用域详解
作用域在JavaScript中是非常重要的概念,理解了它对更深入地理解闭包等概念都有很大的帮助,这篇文章就来谈谈我对作用域的理解. 一.全局作用域与局部作用域 在JavaScri ...
- 贝塞尔曲线 WPF MVVM N阶实现 公式详解+源代码下载
源代码下载 效果图: 本程序主要实现: N阶贝塞尔曲线(通用公式) 本程序主要使用技术 MVVM InterAction 事件绑定 动态添加Canvas的Item 第一部分公式: n=有效坐标点数量 ...