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

题意:给出n,表示接下去给出的字符串长度为n,求最少插入几个字符可以使该字符串变成回文字串。

思路:设原字符串为a,反转该字符串设为b,用字符串长度减去a、b的最长公共子序列即可。

注意:

  1. 若用%c输入,记得getchar
  2. 即使利用了最长公共子序列中后,dp数组开[5050][5050]会造成内存超限,这时候需要利用滚动数组进行处理(结合奇偶性进行重复利用),由于我们只需要求出dp[n][n],所以前面部分的空间其实是可以重复利用的,比如说1->2存完之后,我们需要存3了,但是没有必要再去开辟空间,因为前面1所占的空间之后已经不需要再用了,所以我们可以将3存到1的位置,即3->1,以此类推  4->2。dp只需开到[2][5050]即可,即对每一部分i%2。
  3. 顺带注意区别一下子序列和字串,子序列是不要求满足连续性的,而字串是需要连续的。
 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; char a[],b[];
int dp[][];
int main()
{
int n;
cin>>n;
memset(dp,,sizeof(dp));
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b)); scanf("%s",a); int p=;
for(int i=n-; i>=; i--)
b[p++]=a[i]; for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(a[i]==b[j])
dp[(i+)%][j+]=dp[i%][j]+;
else
dp[(i+)%][j+]=max(dp[i%][j+],dp[(i+)%][j]);
}
}
cout<<n-dp[n%][p]<<endl;
return ;
}

POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)的更多相关文章

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

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

  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 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

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

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

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

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

  6. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  7. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

  9. 周赛F题 POJ 1458(最长公共子序列)

    F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Description ...

随机推荐

  1. 【Dart学习】-- Dart之注释

    Dart支持三种注释类型: 单行注释,多行注释,文档注释. 单行注释单行注释以//开头,从//开始到一行结束的所有内容都会被Dart编译器忽略,示例代码如下: main(){ //打印输出 print ...

  2. 笨办法学Python记录--习题37 异常,lambda,yield,转义序列

    习题中提到了raise,查了下,顺便所有异常类关键字罗列如下文章中: 为什么使用异常 错误处理.事件通知.特殊情况处理.退出时的行为.不正常的程序流程. 简单的示例 在没有任何定义x变量的时候: pr ...

  3. 安装beanstalkd - centos

    安装: wget https://github.com/kr/beanstalkd/archive/v1.9.tar.gz beanstalkd_v1. beanstalkd_v1..tar.gz . ...

  4. 【系统安全性】二、Web攻击与防范

    二.Web攻击与防范 1.XSS攻击 跨站脚本攻击(Cross Site Scripting),因为简写CSS,与层叠样式表(Cascading Style Sheets)有歧义,所以取名XSS 原理 ...

  5. python实现线程池(2.4)

    线程池 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务. 构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...

  6. CSS:CSS Id 和 Class选择器

    ylbtech-CSS:CSS Id 和 Class选择器 1.返回顶部 1. CSS Id 和 Class id 和 class 选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置& ...

  7. Openstack贡献者须知 — OpenPGP/SSH/CLA贡献者协议

    目录 目录 前言 Openstack基金委员会 Openstack贡献者须知 注册Openstack In Launchpad 生成并上传OpenPGP密钥 生成并上传SSH公钥 Join The O ...

  8. python使用threading获取线程函数返回值的实现方法

    python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...

  9. Intel Pin基础

    参考:http://software.intel.com/sites/landingpage/pintool/docs/62732/Pin/html/ http://blog.nruns.com/bl ...

  10. ajax 回传参数

    JSONObject json = new JSONObject(); json.put("msg", msg); json.put("success", co ...