Palindrome 简单(dp)poj1159
2017-11-16 20:29
441 查看
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
Sample Output
Source
以下代码Time超出范围
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
Source
以下代码Time超出范围
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { int n,i,j,k,m,p[5005],d[5005],max; char a[5005],c[5005]; while(cin>>n) { cin>>a; j=0; memset(p,-1,sizeof(p)); memset(d,0,sizeof(d)); for(i=n-1;i>=0;i--) { c[i]=a[j]; j++; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i]==c[j]) { max=-1; //cout<<a[i]<<c[j]<<endl; for(k=0;k<=i;k++) { // cout<<j<<"j"<<k<<"k"<<p[k]<<endl; if(j>p[k]&&max<d[k]) { max=d[k]; m=j; } } if(d[i+1]<max+1||(d[i+1]==max+1&&m<p[i+1])) { d[i+1]=max+1; p[i+1]=m; } } //cout<<d[i+1]<<p[i+1]<<endl; } } max=0; for(i=0;i<=n;i++) { if(max<d[i]) { max=d[i]; } } cout<<n-max<<endl; } return 0; }
以下代码正确
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { int n,i,j,m,dp[2][5005]; char a[5005]; while(cin>>n) { cin>>a; j=0; memset(dp,0,sizeof(dp)); for(i=n-1;i>=0;i--) { for(j=i+1;j<=n;j++) { if(a[i]==a[j]) { dp[i%2][j]=dp[(i+1)%2][j-1]; } else dp[i%2][j]=min(dp[(i+1)%2][j],dp[i%2][j-1])+1; } } cout<<dp[0][n-1]<<endl; } return 0; }
相关文章推荐
- Poj1159 Palindrome 【简单DP】(LCS)
- poj1159 Palindrome (dp)
- POJ1159 Palindrome 简单的DP
- poj1159 Palindrome 区间DP
- POJ-1159-Palindrome(简单dp)
- POJ1159:Palindrome【dp】
- hdu 2955 (简单dp)
- POJ 3186 Treats for the Cows 详解 (简单区间DP)
- HDOJ 4901 - The Romantic Hero 简单dp
- 百练2757:最长上升子序列(简单dp)
- poj 2342 Anniversary party 简单的DP
- HDU 1231 (简单DP问题)
- [区间dp] poj 3280 Cheapest Palindrome
- poj-2955 括号匹配--简单区间dp
- Binary Lexicographic Sequence_简单DP
- UVA 10739--String to Palindrome +dp
- ACM POJ2533 简单dp
- hdu 5092 Seam Carving (简单数塔DP,题没读懂,,不过可以分析样例)
- CF 335B - Palindrome 区间DP
- 简单dp,first wrong POJ 3616 Milking Time