POJ1159——LCS+滚动数组——Palindrome
2015-03-26 14:50
204 查看
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 Source IOI 2000 |
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 5100; int dp[2][MAX]; char s1[MAX],s2[MAX]; int main() { int n; scanf("%d%s",&n,s1); int i,j; for(i = 0,j = n - 1; i < n ; i++,j--) s2[i] = s1[j]; int e = 0; memset(dp,0,sizeof(dp)); for(int i = 0 ; i < n ;i++){ e = 1 - e; for(int j = 0; j < n ;j++){ if(s1[i] == s2[j]) dp[e][j+1] = dp[1-e][j]+1; else dp[e][j+1] = max(dp[e][j],dp[1-e][j+1]); } } printf("%d",n-dp[e] ); return 0; }
View Code
相关文章推荐
- LCS(滚动数组) POJ 1159 Palindrome
- poj-1159-Palindrome【LCS】【滚动数组】
- POJ 1159 Palindrome【LCS+滚动数组】【水题】
- poj 1159 Palindrome 【LCS + 滚动数组】
- POJ 1159 - Palindrome (LCS, 滚动数组)
- poj 1159 Palindrome (DP:LCS+滚动数组)
- hdu1513&&poj 1159 Palindrome(LCS+滚动数组)
- POJ 1159 Palindrome(lcs加滚动数组)
- poj 1159 Palindrome (lcs,滚动数组)
- poj1159 palindrome(动态规划+滚动数组)
- POJ 1159 Palindrome(滚动数组)
- 滚动数组 poj1159 Palindrome
- 动态规划+滚动数组 -- POJ 1159 Palindrome
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
- POJ1159求LCS长度 滚动数组优化空间
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
- poj1159 palindrome(动态规划+滚动数组)
- poj 1159 Palindrome(dp+滚动数组)
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
- dp中滚动数组的应用:01背包、POJ 1159 Palindrome、多段图路径问题