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

【题意】给出一个字符串,求插入多少字符才能形成回文串;

【思路】用a数组存储原串,b数组储存倒串,求最长公共子序列,答案用n-最长公共子序列;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
char a[N],b[N];
//int dp[N][N];//MLE
int dp[][N];//用滚动数组
int main()
{
int n;
while(~scanf("%d",&n))
{
getchar();
scanf("%s",a+);
for(int i=;i<=n;i++)
{
b[n-i+]=a[i];
}
memset(dp,,sizeof(dp));
int mx=;
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i]!=b[j])
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
else dp[i][j]=dp[i-1][j-1]+1;
}
}*/
int e=;//用滚动数组,节约存储空间!!!!
for(int i=;i<=n;i++)
{
e=-e;
for(int j=;j<=n;j++)
{
if(a[i]==b[j])
{
dp[e][j]=dp[-e][j-]+;
}
else
{
if(dp[-e][j]>dp[e][j-])
{
dp[e][j]=dp[-e][j];
}
else dp[e][j]=dp[e][j-];
}
}
}
for(int i=;i<=n;i++)
{
mx=max(mx,max(dp[-e][i],dp[e][i]));
}
int ans=n-mx;
printf("%d\n",ans);
}
return ;
}

Palindrome_滚动数组&&DP的更多相关文章

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

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

  2. HDU 4576 简单概率 + 滚动数组DP(大坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: #include <cstdio> #includ ...

  3. Making the Grade_滚动数组&&dp

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. Gym 100507G The Debut Album (滚动数组dp)

    The Debut Album 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/G Description Pop-group & ...

  5. 【滚动数组】 dp poj 1036

    题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...

  6. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  7. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  8. [POJ1159]Palindrome(dp,滚动数组)

    题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...

  9. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

随机推荐

  1. matlab(函数,变量)

  2. 测试JdbcTemplate执行SQL语句和存储过程

    我在项目中需要使用到oracle的语句片段和存储过程.下面就是我的测试案例: public class DbTest extends BaseTestCase { @Resource JdbcUtil ...

  3. 使用Vibrator控制手机振动

    import android.os.Bundle;import android.os.Vibrator;import android.app.Activity;import android.app.S ...

  4. JavaOne 2016——观众得以一睹JShell的威力

    导读 在JavaOne 2016的主题演讲中,Java平台组的首席架构师Mark Reinhold指出Java 9并不仅仅是Jigsaw,针对Java 9,一共包含了85个JEP.我在这里会关注一个他 ...

  5. HDU 1698 Just a Hook

    题意:初始1-n 值为1,有Q操作,每次可以把一段[l,r] 整段每个值变成 x,问最后的[1,n]总和. 线段树成段更新(基础题) #include<cstdio> #include&l ...

  6. 二模 (10) day2

    第一题: 题目大意:求出区间 [L,R]里约数最多的数.   L,R<=10^9 解题过程: 1.一开始我就往恶心的数据去想了,比如 L=R=一个超级大的质数.. 那么 用搜索质因子的方法  是 ...

  7. 织梦dedecms分类信息模型上一页下一页失效办法

    修改文件/include/arc.archives.class 将一下代码 $next = (is_array($nextR) ? " where arc.id={$nextR['id']} ...

  8. vim配置及插件安装管理(超级详细)

    1 写在前面   Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用 ...

  9. PowerShell并发控制-命令行参数之四问

    传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...

  10. RPI学习--wiringpi_API

    reference: https://projects.drogon.net/raspberry-pi/wiringpi/functions/ Functions (API) Some of the ...