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. 《JavaScript权威指南》读书笔记(三)

    日期:2015-12-05 浏览器location和history: replace不会显示历史,location会: history对象脚本不能真正访问,但支持三种方法:back().foward( ...

  2. Python中T-SNE实现降维

    Python中T-SNE实现降维 from sklearn.manifold import TSNE from sklearn.datasets import load_iris from sklea ...

  3. Jquery异步提交$.ajax的使用

    function test(){ var myEntity=new Object(); myEntity.pro1="xxx"; myEntity.pro2=10; $.ajax( ...

  4. Eclipse导出可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  5. ccpc 2016 省赛

    1.configuration if ide. 2.file import and export. 3.check your program more than once. ============= ...

  6. 为什么要使用 F#?

      对于小部分 .NET 程序员来说,学习一门 .NET Framework 函数化语言无疑将使自己在编写功能强大软件方面前进一大步.而对其他程序员来说,学习 F# 的理由就因人而异了.F# 能为开发 ...

  7. shell中的数组

    在shell脚本中,除了通常使用的shell变量外,有时也需要复杂的数据结构去实现一些功能,这里简单说明一下shell数组的使用方法: 初始化方法 _array_name[0]="rando ...

  8. FFT一周目开坑!

    先来一段非递归! #include<bits/stdc++.h> using namespace std; #define N ((1<<18)+3) ); struct ve ...

  9. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...

  10. 一模 (2) day2

    第一题: 题目大意:给出n种物品和每种物品的件数,求拿k件的方案数.N<=30 解题过程: 1.一开始总想着是组合数学的模型,结果怎么都想不出来..然后写了个爆搜,数据很弱,只有1个点超时. 2 ...