回文字符串

输入一个字符串,可在任意位置添加字符,计算最少再添加几个字符,可以使这个字符串成为回文字符串


int minCost(char a[], int n)
{
    char **buf = new char*[n];
    for (int i = 0; i < n; i++)
    {
        buf[i] = new char[n+1];
    }
    for (int i = 0; i < n; i++)
    {
        buf[i][0] = 0;
        buf[i][1] = 0;
    }
    
    //buf[i][j]表示将从第i个字符开始的长度为j的字符串转换为回文字符串所需添加的字符数量
    for (int i = n-2; i >= 0; i--)
    {
        for (int j = 2; j < n+1-i; j++)
        {
            if (a[i] == a[j-1+i])
            {
                buf[i][j] = buf[i+1][j-2];
            }
            else
            {
                buf[i][j] = buf[i+1][j-1] < buf[i][j-1]? buf[i+1][j-1]+1 : buf[i][j-1]+1;
            }
        }
    }

    int result = buf[0][n];

    for (int i = 0; i < n; i++)
    {
        delete []buf[i];
    }
    delete buf;

    return result;
}







### C++ 实现回文字符串的方法 在处理回文字符串时,可以采用多种方法来判断一个字符串是否为回文。以下是几种常见的实现方式及其对应的代码示例。 #### 方法一:双指针法 通过设置两个指针分别指向字符串的起始位置和结束位置,逐步向中间移动并比较字符是否相等。这种方法的时间复杂度为 O(n),空间复杂度为 O(1)[^2]。 ```cpp #include <iostream> #include <string> bool isPalindrome(const std::string& s) { int left = 0; int right = s.length() - 1; while (left < right) { if (s[left] != s[right]) { return false; // 如果发现不匹配,则不是回文 } ++left; --right; } return true; // 所有字符均匹配 } int main() { std::string str = "level"; if (isPalindrome(str)) { std::cout << "\"" << str << "\" 是回文字符串" << std::endl; } else { std::cout << "\"" << str << "\" 不是回文字符串" << std::endl; } return 0; } ``` --- #### 方法二:反转字符串对比法 将原字符串反转并与原始字符串进行比较。如果两者相同,则说明它是回文字符串。此方法简单易懂,但时间复杂度仍为 O(n),空间复杂度也为 O(n)[^2]。 ```cpp #include <iostream> #include <string> #include <algorithm> // 使用 reverse 函数 bool isPalindrome(const std::string& s) { std::string reversedStr = s; std::reverse(reversedStr.begin(), reversedStr.end()); return s == reversedStr; // 判断原字符串与其反转后的字符串是否一致 } int main() { std::string str = "madam"; if (isPalindrome(str)) { std::cout << "\"" << str << "\" 是回文字符串" << std::endl; } else { std::cout << "\"" << str << "\" 不是回文字符串" << std::endl; } return 0; } ``` --- #### 方法三:动态规划求解最长回文子串 对于更复杂的场景(如寻找最长回文子串),可以通过动态规划解决问题。定义 `dp[i][j]` 表示从索引 i 到 j 的子串是否为回文。状态转移方程如下: - 当 `i == j` 时,单个字符必然是回文; - 当 `j == i + 1` 时,仅需判断这两个相邻字符是否相等; - 对于其他情况,满足条件 `s[i] == s[j] && dp[i+1][j-1]` 即可认为其为回文[^1]。 ```cpp #include <iostream> #include <string> #include <vector> std::string longestPalindromicSubstring(const std::string& s) { int n = s.size(); if (n == 0) return ""; std::vector<std::vector<bool>> dp(n, std::vector<bool>(n, false)); int start = 0, maxLength = 1; for (int i = 0; i < n; ++i) { dp[i][i] = true; // 单个字符必定是回文 } for (int i = 0; i < n - 1; ++i) { if (s[i] == s[i + 1]) { // 邻近两字符相等的情况 dp[i][i + 1] = true; start = i; maxLength = 2; } } for (int length = 3; length <= n; ++length) { // 枚举长度大于等于3的子串 for (int i = 0; i < n - length + 1; ++i) { int j = i + length - 1; if (s[i] == s[j] && dp[i + 1][j - 1]) { dp[i][j] = true; start = i; maxLength = length; } } } return s.substr(start, maxLength); } int main() { std::string input = "babad"; std::string result = longestPalindromicSubstring(input); std::cout << "输入 \"" << input << "\", 最长回文子串为: \"" << result << "\"" << std::endl; return 0; } ``` --- #### 性能分析与总结 上述三种方法各有优劣: - 双指针法适用于简单的回文检测需求,效率高且易于理解。 - 字符串反转法虽然直观,但在某些情况下可能占用更多内存资源。 - 动态规划适合解决较复杂的回文问题(如寻找最长回文子串)。然而,由于需要额外的空间存储 DP 数组,因此可能会增加时间和空间开销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值