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 ...
随机推荐
- HDU 1111 Secret Code (DFS)
题目链接 题意 : 给你复数X的Xr和Xi,B的Br和Bi,让你求一个数列,使得X = a0 + a1B + a2B2 + ...+ anBn,X=Xr+i*Xi,B=Br+Bi*i : 思路 : 首 ...
- 编写高质量代码改善C#程序的157个建议——建议122:以<Company>.<Component>为命名空间命名
建议122:以<Company>.<Component>为命名空间命名 建议以<Company>.<Component>为程序集命名,比如Microso ...
- 通用性站点管理后台(Bee OPOA Platform) (5)- 【扩展】基于WebSocket的监视Sql执行功能
开始 底层的东西总是很类似, 看了园里的Fish-Li的一系列文章, 写得真好, 无论是风格还是内容. 本来也想想方便点就用remoting实现监视功能算了, 但这样就需要一个Winform的项目了. ...
- 从hive将数据导出到mysql(转)
从hive将数据导出到mysql http://abloz.com 2012.7.20 author:周海汉 在上一篇文章<用sqoop进行mysql和hdfs系统间的数据互导>中,提到s ...
- python基本算法
算法优劣评判标准 时间复杂度: 定义:用来评估算法运行效率的一个式子 print('Hello World') O(1) for i in range(n): print('Hello World') ...
- Robotlegs2的Starling扩展
有个老外写了robotleges2的starling扩展,地址是 https://github.com/brean/robotlegs2-starling-viewmap 需要注意的是要先创建一个基于 ...
- UML uml高级知识之用例图
uml高级知识之用例图 建模工具推荐使用 visio2010: include:选择菜单栏中的'UML'->单击’构造型‘->新建->构造型那里输入include->基类那里选 ...
- linux 安装python3.7 报错No module named '_ctypes'
ModuleNotFoundError: No module named '_ctypes' 操作系统:centos yum install libffi-devel ./configure --en ...
- 用Echarts的力向导图可视化数据
学习背景:做一个图论的题目的时候需要将结果可视化来直观的看效果,所以使用Echarts来画.感觉效果不错. Echarts下载地址:https://echarts.baidu.com/download ...
- ocp最新考试题库:052新考题及答案整理-36
36.Which two are true about roles? A) A role can be granted a combination of system and object privi ...