Longest palindrome subsequence

本文介绍了一种高效算法,用于查找给定输入字符串中的最长回文子序列,并提供了具体的实现代码示例。通过动态规划的方法,该算法能有效地解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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() + 1));
    for ( int i = 0; i < strInput.size(); ++ i ) {
        vecVecIntermediate[i][1].strPalindrome = strInput[i];
        vecVecIntermediate[i][1].strFront = "";
        vecVecIntermediate[i][1].strEnd = "";
    }

    for ( int i = 0; i < strInput.size() - 1; ++ i ) {
        if ( strInput[i] == strInput[i + 1 ]) {
            vecVecIntermediate[i][2].strPalindrome = strInput.substr(i, 2 );
            vecVecIntermediate[i][2].strFront = "";
            vecVecIntermediate[i][2].strEnd = "";
        }else {
            vecVecIntermediate[i][2].strPalindrome = strInput[i];
            vecVecIntermediate[i][2].strEnd = strInput[i+1];
        }
    }

    for ( int L = 3; L <= strInput.size(); L += 1 ) {
        for ( int n = 0; n <= strInput.size() - L; ++ n ) {
            size_t findPos;

            Palindrome p2 = vecVecIntermediate[n][L-1];
            p2.strEnd.push_back ( strInput[n + L - 1] );
            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 ( 0, findPos );
                findPos = p2.strEnd.find ( charFind );
                p2.strEnd = p2.strEnd.substr ( findPos + 1 );
            }

            Palindrome p3 = vecVecIntermediate[n+1][L-1];
            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 ( 0, findPos );
                findPos = p3.strEnd.find ( charFind );
                p3.strEnd = p3.strEnd.substr ( findPos + 1 );
            }

            std::vector<Palindrome> vecP{p2, p3};
            int nMaxIndex = 0;
            for ( int index = 1; index < vecP.size(); ++ index ) {
                if ( vecP[index].strPalindrome.length() > vecP[nMaxIndex].strPalindrome.length() ) {
                    nMaxIndex = index;
                }
            }
            vecVecIntermediate[n][L] = vecP[nMaxIndex];
        }
    }
    return vecVecIntermediate[0][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;
    }
}

 

转载于:https://www.cnblogs.com/shengguang/p/8440098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值