递推算法:分割回文串

分割回文串

回文串(palindrome string):指一个镜像对称的字符串

  • abba
  • a
  • asdsa

但如果一个字符串并不是回文串,它可以被视为是一系列回文串的组合:

  • aab = aa + b
  • asdf = a + s + d + f
  • add = a + dd

现在我们要解决:对于一个字符串,其最少要分割几次才能使得得到的子串都是回文串?

我们可以运用递推的方法,来得到一个长为n的串需要分割几次才能全部分割成回文串:

如果记函数f(j)表示前j个字符形成的子串的最小分割数(比如字符串“1234321”的f(6) 为1,会分成“1”、“23432”

则我们要求的就是f(n)

如果这个长为n的串,本身就是一个回文串,则f(n) = 0

如果不是:

  1. 可以视为长为n-1的字符串后面又添了一个字符,要比长为n-1的串多分割一次,即f(n) = f(n-1) + 1
  2. 可能存在某种情况,即从第i个字符到第n个字符是回文串,此时f(n) = f(i-1) + 1

要从这两种情况中取到最小的分割数

至于判断是否是回文串,我在代码中的注释中写出,如下:

​ If we want to know how many times we would cut, we need to know the situation of palindrome in this string.
​ That is, we need to know if any of its segments is palindrome string.
​ Now, we have following ways to judge:

       1. just contain one char
       2. just contain two chars and they are equal
       3. more than three chars and satisfy these following conditions:
          the subString from the second char to the next-to-last char is a palindrome string
          the first and the last char are equal

下面是代码:

int minCut(string s) {
        int length = s.length();
        // if we want to know how many times we would cut, we need to know the situation of palindrome in this string
        // we need to know if any of its segments is palindrome string
        // we have following ways to judge
        // 1. just contain one char
        // 2. just contain two chars and they are equal
        // 3. more than three chars and satisfy these following conditions:
        //         the subString from the second char to the next-to-last char is a palindrome string
        //         the first and the last char are equal

        vector<vector<bool>> palindromic(length, vector<bool>(length));

        for(int i = 0 ; i < length ; i++){
            for(int j = i ; j >= 0 ; j--){
                if(i == j){
                    palindromic[j][i] = true;
                }else if(i - j == 1){
                    if(s[i] == s[j]){
                        palindromic[j][i] = true;
                    }
                }else{
                    if(s[i] == s[j] && palindromic[j+1][i-1]){
                        palindromic[j][i] = true;
                    }else{
                        palindromic[j][i] = false;
                    }
                }
            }
        }

        vector<int> cutTimes(length);

        for(int i = 0  ; i < length ; i ++){
            if(palindromic[0][i])cutTimes[i] = 0;
            else{
                cutTimes[i] = cutTimes[i - 1] + 1;
                for(int j = 1 ; j < i ; j++){
                    if(palindromic[j][i])
                        cutTimes[i] = cutTimes[j - 1] + 1 < cutTimes[i] ? cutTimes[j - 1] + 1 : cutTimes[i];
                }
            }
        }

        return cutTimes[length - 1];
    }

时间复杂度和空间复杂度皆为O(n²)

代码截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值