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(最长公共子序列+滚动数组)的更多相关文章

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

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  2. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  3. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  4. Palindrome(最长公共子序列)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48526   Accepted: 16674 Description A p ...

  5. POJ 1159:Palindrome 最长公共子序列

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

  6. poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53414   Accepted: 18449 Desc ...

  7. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  8. 2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组)

    2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组) https://www.luogu.com.cn/problem/P2516 题意: 给定字符串 \(S\) ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

随机推荐

  1. maven入门程序(3)

    欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3472864.html 这里我将展示如何用maven快速创建一个基于spring的web框架.创建步骤和上一篇 ...

  2. [android网络有效性检测] NetworkMonitor代码造成内存泄漏

    造成内存泄漏的log如下: E StrictMode: A resource was acquired at attached stack trace but never released. See ...

  3. iOS进阶——App生命周期

    State Description Not running The app has not been launched or was running but was terminated by the ...

  4. gitignre

    1.配置语法: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录: PLAC ...

  5. Web性能压力测试工具之Siege详解

    PS:Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进 ...

  6. Jxl操作excel的demo

    网上很多例子,都是用Jxl读或者写excel,本文实现的功能就是将数据源in.xls的第几行第几列数据写入到out.xls的第几行第几列,不覆盖out.xls其他原有的数据. 需要导入的包:jxl.j ...

  7. HTML中href的链接刷新页面问题

    在上一篇随笔中说到了html()方法不能一直改变标签的值的问题,当单击完成时,回调函数返回的值瞬间就没有了,今天突然想到了,我单击的是链接啊,就算链接到本界面上,也要进行刷新,页面一刷新,显示的值自然 ...

  8. Hive[5] HiveQL 数据操作

    5.1 向管理表中装载数据   Hive 没有行级别的数据插入更新和删除操作,那么往表中装载数据的唯一途径就是使用一种“大量”的数据装载操作,或者通过其他方式仅仅将文件写入到正确的目录下:   LOA ...

  9. VS2010中无法嵌入互操作类型“......”,请改用适用的接口的解决方法

  10. 【BZOJ 1015】[JSOI2008]星球大战starwar

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...