HDU 1513 Palindrome(最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513

解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个最少的个数是多少?

一开始以为只是一个普通的DP题,但是按照我的想法敲出来之后怎么样都W了,无奈搜了解题报告,得知其实这个就是一个最长公共子序列问题,就是求这个字符串跟它的逆序的

字符串的最长公共子序列。因为杭电的题内存都要求在32M内存以内,所以很开心的敲出来才发现10^6的数组都开不了,所以只好寻求空间压缩的方法,一开始就觉得是用滚动数组,但看了很久就是没看出来。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn = 5005;

int dp[2][maxn];
char str[maxn];

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",str+1);
memset(dp,0,sizeof(dp));
int flag = 1;
for(int i = 1;i <= n;++i)
{
flag = !flag;
for(int j = 1;j <= n;++j)
{
if(str[i] == str[n-j+1])
dp[flag][j] = dp[!flag][j-1]+1;
else dp[flag][j] = max(dp[!flag][j],dp[flag][j-1]);
}
}
printf("%d\n",n-dp[flag][n]);
}
return 0;
}

HDU 1513 Palindrome(最长公共子序列)

时间: 2024-10-09 21:37:07

HDU 1513 Palindrome(最长公共子序列)的相关文章

!HDU 1513 Palindrome--dp--(最长公共子序列模型)

题意:给定一个字符序列,求最少添加多少个字符能让它变成对称序列 分析:这题的做法竟然是把序列颠倒之后求最长公共子序列,然后n-dp[n][n]就是答案.记住这种做法. 在这里再说一次最长公共子序列的做法:dp[i][j]表示序列1的前i个字符和序列2的前j个字符比较时的最长公共子序列的长度,状态转移公式:1.当a[i]==b[j]时,dp[i][j]=dp[i-1][j-1]+1:2.否则,dp[i][j]=max(dp[i-1][j],dp[i][j-1]) 代码: #include<iost

poj1159 Palindrome(最长公共子序列)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52966   Accepted: 18271 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

POJ1159——Palindrome(最长公共子序列+滚动数组)

Palindrome DescriptionA 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 inser

HDU 1159 LCS最长公共子序列

1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 const int N = 1005; 6 #define max(a,b) a>b?a:b 7 8 char a[N] , b[N]; 9 int dp[N][N]; 10 11 int main() 12 { 13 while(scanf("%s%s" , a+1 , b+1) != EOF){ 14 int l1 =

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

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

hdu 1159 common sequence (最长公共子序列 dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意 : 给出两个字符串 求出最长公共子序列 思路: if(str1[i]==str2[j]) { dp[i][j]=max(dp[i-1][j-1]+1,max(dp[i-1][j],dp[i][j-1])); } else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); #include<cstdio> #include<cstring> #include&l

hdu 1159 Common Subsequence(最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37551    Accepted Submission(s): 17206 Problem Description A subsequence of

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p