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的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...
随机推荐
- 在Centos6.5安装MySQL
在阐述安装过程之前一定要再写一句,Centos7上安装MySQL是默认安装的MariaDB,因为在Centos7中用MariaDB代替了mysql数据库. linux安装软件很简单,有的时候只有一句话 ...
- NSS_03 过滤器
asp.net mvc3有四类过滤器:授权, 操作,结果, 异常.操行的顺序为:授权,操作,结果,异常. 首先看一下TempData: 数据只能经过至多一次的Controller传递, 并且每个元素至 ...
- JQGrid+Nhibernate+Webservice+Linq
先上效果图: 前台代码(jqgridtest.aspx): <%@ Page Language="C#" AutoEventWireup="true" ...
- log4net基本日志使用笔记[windows application]
Ref: http://www.cnblogs.com/wangsaiming/archive/2013/01/11/2856253.html http://www.cnblogs.com/zhouf ...
- EF迁移
第一步是为上下文启用迁移Enable-Migrations 命令 Add-Migration AddBlogUrl 将根据自创建上次迁移以来您对模型所做的更改,为下一次迁移搭建基架. Update-D ...
- C#对象转JSON字符串和JSON字符串转对象
namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...
- 九度OJ1172--哈夫曼树
哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有多组数据.每 ...
- 【Delphi】窗体阴影
procedure TForm1.FormCreate(Sender: TObject); begin SetClassLong(Handle, GCL_STYLE, GetClassLong(Han ...
- 1062 Talent and Virtue (25)
/* L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of ta ...
- (转)《深入理解java虚拟机》学习笔记2——Java内存溢出实例
通过简单的小例子程序,演示java虚拟机各部分内存溢出情况: (1).java堆溢出: Java堆用于存储实例对象,只要不断创建对象,并且保证GC Roots到对象之间有引用的可达,避免垃圾收集器回收 ...