Palindrome (动态规划) 构成回文串

F - Palindrome

Time Limit:3000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
 
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

Hint

 

本题最需注意的就是数组类型要用short 型!!!

 

方法一:构造最长子串
给一个字符串,求这个字符串最少增加几个字符能变成回文。
如Ab3bd可以增加2个字符变为回文:Adb3bdA。
通过这样的结论可以和最长公共子串联系起来:
S和S' (注:S'是S的反串)的最长公共子串其实一定是回文的。
即用s的长度减去最大子串的长度即为所求值。
#include <iostream>//49248kb 985ms AC
using namespace std;
char a[5002];
char b[5002];
short f[5002][5002];
int main()
{
	int i, j, n;
	int alen, blen;
	scanf("%d",&n);
	scanf("%s",a+1);
	alen = strlen(a+1);
	blen = strlen(a+1); 
	for(i=1;i<=alen;i++)
		b[alen-i+1]=a[i]; //构造a字符串的反字符串b
	for( i=0; i<=alen; i++ )
		for ( j=0; j<alen; j++ )
			f[i][j] = 0; //初始化f数组

	for( i=1; i<=alen; i++ )
		for( j=1; j<=blen; j++ ){
			if( a[i]==b[j] )
				f[i][j] = f[i-1][j-1]+1;
			else
				f[i][j] = f[i-1][j]>f[i][j-1]?f[i-1][j]:f[i][j-1]; 
		} //求出最大子串长度
	cout << alen-f[alen][blen] << endl;
   	/*for( i=1; i<=alen; i++ ){
		for( j=1; j<=blen; j++ )
			cout << f[i][j] << " ";
		cout << endl;
	}*/
	//输出 f 数组		
    return 0;
}

方法二:用经典动态规划的方法
设dp[i][j]存储的是字符串i到j之间至少插入的字符数量,其值通过以下方程求得,

if(str[i] == str[j])
 dp[i][j] = dp[i+1][j-1];
else
 dp[i][j] = min(1+dp[i][j-1], 1+dp[i+1][j]);

举例解释如下。对于字符串“Ab3bd",其下标为0,1,2,3,4,题目所求即dp[0][4]。比较str[0]与str[4],不相等。故肯定要在字符串前面添加str[4],或者在字符串后面添加str[0]。对于第一种情况,其所需最小添加字符数为1+dp[0][3];对于后面一种情况,其所需最小添加字符数为1+dp[1][4]。其中的“1”代表的是在首部在尾部添加的那个元素。对于str[i]==str[j]的情况比较好理解,就不说明了。
#include <iostream>//40732kb 407ms AC
#include <string>

using namespace std;

const int MAX_SIZE = 5005; //最大字符串长度
short dp[MAX_SIZE][MAX_SIZE]; //dp[i][j]存储字符串[i, j]要插入的字符数,使用得其成为“回文”

int getMin(int a, int b) {
    return a < b ? a : b;
}

int main()
{
    int n;
    cin >> n;
    string str;
    cin >> str;

    /*循环求得的是一个右上角矩阵,其余元素为0*/
    for(int i = n - 1; i >= 0; i--) {
        for(int j = i; j <= n - 1; j++) {
            if(str[i] == str[j])
                dp[i][j] = dp[i + 1][j - 1];
            else
                dp[i][j] = getMin(1 + dp[i][j-1], 1 + dp[i+1][j]);
        }
    }

    cout << dp[0][n-1] << endl;

    return 0;
}

Please enable JavaScript to view the comments powered by Disqus.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值