Palindrome
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 56150 | Accepted: 19398 |
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
给一个字符串,计算最少加多少个字符可以使字符串变成回文串(即从前往后读与从后往前读一样)。
有2种思路。一种是直接区间DP。dp[j][i]表示[i,j]这个子串要变成回文串须要加入多少个字符,状态转移方程例如以下:if(s[i]==s[j])
dp[j][i]=dp[j+1][i-1];
else
dp[j][i]=1+min(min[j+1][i],min[j][i-1])
另外一种思路也比較easy想,要将一个字符串变为回文串,那么我们就能够先得到这个字符串的逆序串,然后再求出这两个的最长公共子序列,要加入的字符数就是字符串长度减去最长公共子序列的长度。
另外,这道题还会限制内存。
假设定义一个5000*5000的数组会Memory Limit Exceeded。有两种解决方式。一是把数组定义成short型。这样原本的内存会降低非常大一部分,大概会在50000kb左右,刚好可以AC;还有一种解决方式:由于计算第i行时仅仅须要知道第i-1行。所以可以开一个2*5000的滚动dp数组。这样的方法比較推荐。非常节省内存。
/* LCS+short Memory: 49688 KB Time: 1094 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; const double eps=1e-6; const int MAXN=5001; char s[MAXN],t[MAXN]; int n,ans,tlen; short int dp[MAXN][MAXN]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(scanf("%d",&n)!=EOF) { scanf("%s",s+1); for(int i=n;i>=1;i--) t[n-i+1]=s[i]; t[n+1]=0; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(s[i]==t[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); printf("%d ",n-dp[n][n]); } return 0; }
/* DP+short Memory: 55716 KB Time: 1454 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; const double eps=1e-6; const int MAXN=5300; char s[MAXN]; short int n,dp[MAXN][MAXN]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(scanf("%d",&n)!=EOF) { scanf("%s",s); memset(dp,0,sizeof(dp)); for(int i=1;i<n;i++) for(int j=i-1;j>=0;j--) { if(s[i]==s[j]) dp[j][i]=dp[j+1][i-1]; else dp[j][i]=(short int)(min(dp[j+1][i],dp[j][i-1])+1); } printf("%d ",dp[0][n-1]); } return 0; }
/* LCS+滚动数组 Memory: 728 KB Time: 735 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; const double eps=1e-6; const int MAXN=5001; char s[MAXN],t[MAXN]; int n,ans,tlen,dp[2][MAXN]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(scanf("%d",&n)!=EOF) { scanf("%s",s+1); for(int i=n; i>=1; i--) t[n-i+1]=s[i]; t[n+1]=0; memset(dp,0,sizeof(dp)); int indexs=0; for(int i=1; i<=n; i++) { indexs=!indexs; for(int j=1; j<=n; j++) if(s[i]==t[j]) dp[indexs][j]=dp[!indexs][j-1]+1; else dp[indexs][j]=max(dp[!indexs][j],dp[indexs][j-1]); } printf("%d ",n-dp[indexs][n]); } return 0; }