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. UVaLive6834 Shopping

    题意:一条直线上有n个点,标号从1到n,起点为0,终点为N+1,给定m个限制关系(ci,di),访问ci点前必须先访问di点,每两个点之间是单位距离,求在限制条件下,从起点到终点访问完所有的点的最短距 ...

  2. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  3. Libcurl笔记五_easy模式运行原理

    1, curl_easy_init内部调用Curl_open创建一个结构体SessionHandle(里面包含了所以curl使用的数据和指针)并初始化一些数据,然后返回将其作为给外侧使用的句柄CURL ...

  4. Nodejs微信接口

    代码重要部分都已详细注释,test.js为实例,如果启动url请求,那么程序默认对json格式数据友好,如果有特殊需要,请自行修改返回数据的处理格式 大概功能简介为下: this._token 提供t ...

  5. PostgreSQL 8.1 中文文档

    PostgreSQL 8.1 中文文档 http://www.php100.com/manual/PostgreSQL8/

  6. PHP基础入门教程 PHP循环函数

    PHP循环主要有四种:while,do…while,for,foreach.下面我们分开讲解每种循环的用法. while语句: 只要指定的条件成立,则循环执行代码块. 格式: while(expr) ...

  7. 《PHP与MySQL WEB开发》读书笔记

    <PHP与MySQL WEB开发>读书笔记 作者:[美]Luke Welling PHP输出的HereDoc语法: echo <<<theEnd line 1 line ...

  8. Iso8601 日期格式

    unit Iso8601Unit; interface type TIso8601 = class(TObject) public class function DateTimeFromIso8601 ...

  9. mysql_sql语句之美

    无线地址及数量统计 单个用户无线登录信息统计

  10. Cocos2d-x第一个坑,NDK 编译环境

    这些天搭建windows cocos2d-x的环境,基本上崩溃到死.目前好转.终于可以编译通过: 生成模板工程:在cmd下进入cocos2d-x的主目录,D:\Android\cocos2d-x-2. ...