Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 59101   Accepted: 20532

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
 /* 判断最少添加的字符数目,这个用for顺序太难写了,我就用记忆化搜索,因为这个题目内存是65MB,5000*5000会超过限制,short int在数据不超的情况下,可以节约空间,sizeof(int)=4,而sizeof(short)=2
DP方程:if(s[i]==s[j])f[i][j]=f[i+1][j-1]
else f[i][j]=min(f[i+1][j],f[i][j-1])+1;
其中f[i][j]表示i--j这个区间形成回文串的最少添加字符数目
*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 5003
short f[N][N]={};
char s[N];
int n;
int search(int l,int r)
{
if(f[l][r])return f[l][r];
if(l==r-)
{
if(s[l]==s[r]) return f[l][r]=;
return f[l][r]=;
}
if(l==r)
return f[l][r]=;
if(s[l]==s[r])
return f[l][r]=search(l+,r-);
else
{
return f[l][r]=min(search(l+,r),search(l,r-))+;
}
}
int main()
{
scanf("%d%s",&n,s+);
cout<<search(,n)<<endl;
return ;
}

线性DP POJ 1159 Palindrome的更多相关文章

  1. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  2. poj 1159 Palindrome(区间dp)

    题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...

  3. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

  4. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

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

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

  6. HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)

    题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...

  7. OpenJudge/Poj 1159 Palindrome

    1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...

  8. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

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

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

随机推荐

  1. javascript 中的 this 关键字详解

    1.javascript 中 什么是 this? this 指的是当前行为执行的主体,或者是当前方法执行的主体 context:是当前行为或者方法执行的环境 实例: xx 去北京饭店吃东西:上下文是“ ...

  2. python算法之近似熵、互近似熵算法

    理论基础 近似熵? 定义:近似熵是一个随机复杂度,反应序列相邻的m个点所连成折线段的模式的互相近似的概率与由m+1个点所连成的折线段的模式相互近似的概率之差. 作用:用来描述复杂系统的不规则性,越是不 ...

  3. 使用批处理方式从svn 检出DEMO

    Branching in Subversion¶ FROM:https://dev.geogebra.org/trac/wiki/SubversionBranching Some people wan ...

  4. 用C#实现对MSSqlServer数据库的增删改查---DAL层

    说明:本人完成的工作是对传感器--超声波物位计进行硬件集成,上位机通过串口接收传感器数据并将其存到数据库中:在DAL层实现对数据库的增删改查,其中包含两个数据表分别是WaterLevelSet表和Wa ...

  5. OAuth认证与授权

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...

  6. 一致性hash理解

    在做memcached分布式集群时往往要用到一致性hash算法来调节缓存数据的分布. 通常的hash算法是以服务器数量N作为模数,使用key%N的值来获得最终位置,显然当服务器数量发生变化即N发生变化 ...

  7. html基础-css-选择器

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. ECMA6

    let关键字 用来替代var 的关键字,不能重复定义一个变量 举例: for(var i=0; i<5; i++){      setTimeout(function(){           ...

  9. Validating a Model

  10. QT编译发布程序后报错如缺少dll、“应用程序无法正常启动(0xc000007b)”的可能解决方法

    QT编译发布程序后报错如缺少dll.“应用程序无法正常启动(0xc000007b)”的可能解决方法 最近项目要用qt,因为初学没有经验,遇到些小问题常常没什么头绪,也查不到解决方法,刚刚还因为低端错误 ...