Palindrome
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 43607 | Accepted: 14862 |
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.
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
问题分析: 经典DP问题 两种思路
思路一:
回文串:要求得到一个字符串变为回文串要插入字符的最少数
输入a字符串的长度,再输入a字符串;
将a字符串复制到b中(相当于将a字符串反置)
求出其与a的最长公共子序列len;
则min=strlen(a)-len
Eg.
A: Ad3db
B: bd3dA
最长公共子序列为d3d
所以len=3;
Min=n-len=2
代码实现:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 using namespace std; 5 short dp[5005][5005]; 6 char a[5005]; 7 char b[5005]; 8 int main(){ 9 int n; 10 while(scanf("%d",&n)!=EOF){ //cin the lenth of a 11 scanf("%s",a+1); 12 strcpy(b,a+1); //copy a to b 13 for(int i=0;i<=n;i++){ 14 dp[i][n]=0; 15 } 16 for(int j=0;j<=n;j++){ 17 dp[0][j]=0; 18 } 19 for(int i=1;i<=n;i++){ //let b oppsite to get it and a's the longest common lenth 20 for(int j=n-1;j>=0;j--){ 21 if(a[i]==b[j]){ 22 dp[i][j]=dp[i-1][j+1]+1; 23 } 24 else{ 25 dp[i][j]=((dp[i-1][j])>(dp[i][j+1]))?(dp[i-1][j]):(dp[i][j+1]); 26 } 27 } 28 } 29 int res=n-dp[n][0]; 30 printf("%d\n",res); 31 } 32 return 0; 33 }
思路二:
回文串:
输入a串,求其变成回文串所要进行的插入或删除操作的最少的步骤
Dp[i][j] 为a[i~j] 子串变为回文串所要进行插入或删除的操作的最少步骤。
Min{dp[i][j-1]+1, dp[i-1][j]+1} (a[i]!=a[j])
Dp[i][j]=
Min{dp[i][j-1]+1,dp[i-1][j]+1,dp[i-1][j-1]} (a[i]==a[j])
则最后答案为dp[1][n]
代码实现:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 using namespace std; 5 char a[5001]; 6 short dp[5001][5001]; //dp[i][j] means a[i~j] become return string needs to insert or delete's min operate number 7 int min1(int b,int c,int d){ 8 int temp=b<c?b:c; 9 temp=temp<d?temp:d; 10 return temp; 11 } 12 int min2(int e,int f){ 13 int temp=e<f?e:f; 14 return temp; 15 } 16 int main(){ 17 int n; 18 while(scanf("%d",&n)!=EOF){ 19 scanf("%s",a+1); //cin a string 20 int len=strlen(a+1); 21 for(int i=0;i<=len;i++){ 22 dp[i][i]=0; 23 } 24 for(int j=1;j<=len-1;j++){ 25 for(int k=1;k<=len-j;k++){ 26 int g=k+j; 27 if(a[g]==a[k]){ 28 dp[k][g]=min1(dp[k+1][g-1],dp[k+1][g]+1,dp[k][g-1]+1); 29 } 30 else{ 31 dp[k][g]=min2(dp[k][g-1]+1,dp[k+1][g]+1); 32 } 33 } 34 } 35 printf("%d\n",dp[1][len]); 36 } 37 return 0; 38 }