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
n | 4 |
---|---|
m | 4 |
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.
⎩⎨⎧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])(其他)
这个递推公式可以用O(nm)计算出来,dp[n][m]就是LCS的长度。
j\i | 0 | 1(b) | 2(e) | 3(c ) | 4(d) |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1(a) | 0 | 0 | 0 | 0 | 0 |
2(b) | 0 | 1 | 1 | 1 | 1 |
3(c ) | 0 | 1 | 1 | 2 | 2 |
4(d) | 0 | 1 | 1 | 2 | 3 |
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;
}
实践才是检验真理的唯一标准;