POJ - 1159 Palindrome (回文串与最长公共子序列问题)

Problem Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string “Ab3bd” can be transformed into a palindrome (“dAb3bAd” or “Adb3bdA”). However, inserting fewer than 2 characters does not produce a palindrome.

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from ‘A’ to ‘Z’, lowercase letters from ‘a’ to ‘z’ and digits from ‘0’ to ‘9’. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

此题只需求出正反序列的最长公共子序列即可,这里简单介绍一下最长公共子序列

就举出一个题目作为出发点;

最长公共子序列

假如给定两个字符串s1s2······sn和t1t2·····tn。求出这两个字符串最长公共子序列长度。字符串s1s2······sn的子序列值可以表示为si1si2······sim(i1<i2<·······<im)的序列。(1<=n,m<=1000)

Sample Input

n4
m4
s“abcd”
t“becd”

Sample Output

3  //("bcd")

这个问题是被称为最长公共子序列问题(LCS,Longest Common Subsequence)的著名问题。可以用如下方式定义:

dp[i][j]:=s1·····si和t1·····tj对应的LCS长度
由此,s1····si+1和t1·····tj对应的公共子序列可能是
当si+1=tj+1时在s1·····si和t1····tj的公共子序列末尾加上si+1
s1·····si和t1·····tj的公共子列
s1·····si+1和t1·····tj+1的公共子列

三者中的某一个,所以就有如下的递推关系成立
dp[i+1][j+1]= { m a x ( d p [ i ] [ j ] + 1 , d p [ i ] [ j + 1 ] , d p [ i + 1 ] [ j ] ) . ( s   i + 1   = t   j + 1   ) m a x ( d p [ i ] [ j + 1 ] , d p [ i + 1 ] [ j ] ) ( 其 他 ) \left\{\begin{array}{c}max(dp[i][j]+1,dp[i][j+1],dp[i+1][j]) .(s~i+1~=t~j+1~)\\ \\ max(dp[i][j+1],dp[i+1][j])(其他)\end{array}\right. maxdp[i][j]+1,dp[i][j+1],dp[i+1][j].(s i+1 =t j+1 )maxdp[i][j+1],dp[i+1][j]()

这个递推公式可以用O(nm)计算出来,dp[n][m]就是LCS的长度。

j\i01(b)2(e)3(c )4(d)
000000
1(a)00000
2(b)01111
3(c )01122
4(d)01123
DP数组
int n,m;
char s[MAX_N],t[MAX_M];

int dp[MAX_N+1][MAX_M+1];    //DP数组
void solve()
{
     for(int i=0;i<n;i++)
     {
          for(int j=0;j<m;j++)
          {
                if(s[i]==t[j])
                {
                       dp[i+1][j+1]=dp[i][j]+1;
                }
                else 
                {
                    dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
                }
          }
     }
      printf("%d\n",dp[n][m]);
}

为什莫说只需求出正反序列呢?我们都知道回文串是一个对称字符串所以从前往后和从后往前看都是一样的所以正反序列也是一样的,根据题目要求我们应当求出它们不一样的单词个数也就是求出原字符串长度减去它们的最长公共序列长度的个数

特别注意:

这里的数组(5000*5000)过大不能直接进行定义否则会内存超限;但是我们申请的内存空间可以循环利用的哦(就是滚动数组哦),看完代码你就会明白了
代码如下

#include<iostream>
#include<cstring>
using namespace std;
char a[5010],b[5010];
int dp[5][5010];//其实只需要
int main()
{
    int n;
    while(cin>>n)
    {
        int k=n-1;
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
            b[k--]=a[i];
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(a[i]==b[j])
                    dp[(i+1)%2][j+1]=dp[i%2][j]+1;  //余2因为每次循环第i次的时候第i-1的结果
                                          //都会储存到第i次所以进行比较的只有i和i-1(也就是0和1)的结果;
                else
                    dp[(i+1)%2][j+1]=max(dp[i%2][j+1],dp[(i+1)%2][j]);  //因此只需要2个空间;
            }
        }
        if(n%2==0)
        cout<<n-dp[0][n]<<endl;
        else
            cout<<n-dp[1][n]<<endl;
    }
    return 0;
}

实践才是检验真理的唯一标准;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值