POJ1159——Palindrome(最长公共子序列+滚动数组)
Palindrome
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
题目大意:
给定一个字符串,判断最少需要添加几个字符使其变成回文。
解题思路:
回文定义:一个字符串与该字符串的逆序相同。
则显然该题求得是:字符串长度-字符串与其逆序的最长公共子序列长度
字符串有5000位,若使用一般的LCS需要开5000*5000的数组。(数组过大)
所以需要使用滚动数组。(LCS每行更新的时候只需要根据上一行的数据即可) 只需要开辟2*5000的数组。 ^_^
LCS的dp方程:dp[i][j]=max( dp[i-1][j-1] + (a[i]==b[j]) , max( dp[i-1][j] , dp[i][j-1] ) )
Code:
/*************************************************************************
> File Name: poj1159.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月27日 星期一 20时44分15秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 5001
using namespace std;
char a[MAXN], b[MAXN];
int dp[][MAXN];
int main()
{
int N;
while(cin>>N)
{
for (int i=;i<=N;i++)
{
scanf("%c",&a[i]);
b[N-i+]=a[i];
}
memset(dp,,sizeof(dp));
for (int i=;i<=N;i++)
{
dp[][]=dp[][];
for (int j=;j<=N;j++)
{
int tmp=max(dp[][j-],dp[][j]);
if (a[j]==b[i])
tmp=max(tmp,dp[][j-]+);
dp[][j]=tmp;
}
for (int i=;i<=N;i++)
dp[][i]=dp[][i];
}
cout<<dp[N][N]<<endl;
}
return ;
}
POJ1159——Palindrome(最长公共子序列+滚动数组)的更多相关文章
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- Palindrome(最长公共子序列)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 48526 Accepted: 16674 Description A p ...
- POJ 1159:Palindrome 最长公共子序列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56273 Accepted: 19455 Desc ...
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53414 Accepted: 18449 Desc ...
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组)
2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组) https://www.luogu.com.cn/problem/P2516 题意: 给定字符串 \(S\) ...
- HDU 1513 Palindrome(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...
随机推荐
- how to Enable Client Integration
i got a problem,the problem is list cant use export to excel button in sharepoint 2010. I found my a ...
- DEDECMS中,引入文件
引入文件:dede:include 标签:{dede:include filename="foot.htm"/}
- L007-oldboy-mysql-dba-lesson07
L007-oldboy-mysql-dba-lesson07 [root@web01 ~]# mysqldump -uroot -ptestpassword -A >/root/mysql_ba ...
- Spring PecClinic宠物医院---安装
1.下载源代码 如果本地安装了Git工具,可以直接使用命令 git clone https://github.com/spring-projects/spring-petclinic.git 如果没有 ...
- 如何建立一个“绑定友好的”usercontrol--wpf
如何建立一个"绑定友好的"usercontrol--wpf 这几天在打算将以前用winform写的工具程序重构到wpf,顺便学习理解看过的wpf的知识. 因为程序设计到一个Exce ...
- Jquery实现手机上下滑屏滑动的特效代码
要引入两个jquery插件 可以去网上下载 <script src="jquery-1.11.1.min.js"></script><script s ...
- EditorLineEnds.ttr 受影响的D版本 Delphi 8-2010
http://stackoverflow.com/questions/25295980/delphi-2006-2010-error-cannot-create-file-c-users-admin- ...
- CorelDRAW 插件的安装和使用
CorelDRAW 是一款在中国非常受欢迎的图形软件,开放的界面和编程技术,能够对它进行二次开发制作插件,插件大抵有三种gms.cpg.exe格式,下面介绍一下这三种插件的安装和使用方法. 一.gms ...
- java-----基本数据类型包装类
目的:为了方便操作基本数据类型值,将其封装为对象,在对象定义了属性和行为,丰富了改数据的操作,用于描述该对象的类也就成为基本数据类型对象包装类. 例如:int类型的取值范围:Integer------ ...
- EXTJS 3.0 资料 控件之 combo 用法
EXTJS combo 控件: 1.先定义store //年款 var comboData_ReleasYear = [ ['], ['], ['], ['] ]; 2.定义combo控件 { lay ...