Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4751    Accepted Submission(s): 1625

Problem 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
 
Source
 
这题用区间DP会爆内存。。正确解法利用LCS解,并且LCS算法算的时候只和当前行和上一行有关,所以利用滚动数组压缩空间
///题意:将一个字符串变成回文串需要添加的最少字符数量
///解法:将字符串反过来.求出原字符串与现在的字符串的LCS,然后用原串减掉LCS的长度即为最少添加的字符
///这个解法的好处就是可以将DP数组变成滚动数组
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
int dp[][N];
char str[N],str1[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",str+);
for(int i=;i<=n;i++){
str1[n-i+]=str[i];
}
///printf("%s",str1+1);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(str[i]==str1[j]){
dp[i%][j] = dp[(i-)%][j-]+;
}else{
dp[i%][j] = max(dp[i%][j-],dp[(i-)%][j]);
}
}
}
printf("%d\n",n-max(dp[][n],dp[][n]));
}
}

贴个爆内存的代码。。开short都没用,hdu数据强吗 ORZ ~~~

///题意:将一个字符串变成回文串需要添加的最少字符数量
///解法:区间DP,dp[i][j]代表区间i-j里面要添加多少个字符才能将其变成回文串
///如果 str[i] == str[j] 那么 dp[i][j] = dp[i+1][j-1]
///否则 dp[i][j] = min(dp[i+1][j] ,dp[i][j-1])+1 即考虑是在第i+1个字符之前添加str[j]还是在
///在j-1之后添加字符str[i]
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
short dp[N][N];
char str[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",str);
for(int i=;i<=n;i++) dp[i][i]=;///区间长度为1的时候不需要添加就是回文串
for(int l = ;l<=n;l++){
for(int i=;i<=n-l+;i++){
int j = i+l-;
if(str[i]==str[j]) {
dp[i][j] = dp[i+][j-];
}else{
dp[i][j] = min(dp[i+][j],dp[i][j-])+;
}
}
}
printf("%d\n",dp[][n-]);
}
}

hdu 1513(滚动数组)的更多相关文章

  1. hdu 1024(滚动数组+动态规划)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. HDU - 3033 滚动数组有坑

    每层至少一个,滚动时要判上一层非法与否,所以每次都要memset #include<bits/stdc++.h> #define rep(i,j,k) for(int i=j;i<= ...

  3. hdu 1513(dp+滚动数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 思路:n这么大,可以采用滚动数组,然后就是求原串和反串的LCS了. #include<io ...

  4. hdu 1513 Palindrome【LCS滚动数组】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

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

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

  7. hdu 1513 添最少字回文 (Lcs 滚动数组)

    http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表 ...

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

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

  9. hdu 4576 (简单dp+滚动数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 题意:给出1~n的环,m个操作,每次能顺时针或逆时针走w步,询问最后在l~r这段区间内概率.(1 ...

随机推荐

  1. Python标准模块logging

    http://blog.csdn.net/fxjtoday/article/details/6307285 开发Python, 一直以来都是使用自己编写的logging模块. 比较土...... 今天 ...

  2. 【翻译】介绍 ASP.NET Core 中的 Razor Pages

    介绍 ASP.NET Core 中的 Razor Pages 原文地址:Introduction to Razor Pages in ASP.NET Core         译文地址:介绍 asp. ...

  3. lintcode-119-编辑距离

    119-编辑距离 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=&q ...

  4. windows服务那些事

    前一段时间由于项目需求,写了一个windows服务.下面总结如下: windows服务其实就是一些后台程序,和其他程序的主要区别是它运行于系统后台.微软公司为了方便我们自己定制我们的服务,提供了很多借 ...

  5. [剑指Offer] 15.反转链表

    题目描述 输入一个链表,反转链表后,输出链表的所有元素. [思路1]三个指针在链表上同时滑动. /* struct ListNode { int val; struct ListNode *next; ...

  6. [LeetCode] 70. Climbing Stairs(斐波那契数列)

    [思路] a.因为两种跳法,1阶或者2阶,那么假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法是f(n-1); b.假定第一次跳的是2阶,那么剩下的是n-2个台阶,跳法是f(n-2) c.由a.b ...

  7. (转)Foundation-性能优化之NSDateFormatter

    性能优化之NSDateFormatter 为什么要优化NSDateFormatter? 首先,过度的创建NSDateFormatter用于NSDate与NSString之间转换,会导致App卡顿,打开 ...

  8. NOIP模板

    快排 procedure qsort(l,r:longint); var i,j,t,m:longint; begin i:=l; j:=r; m:=a[(i+j) ]; repeat while a ...

  9. hdu 1575 Tr A (二分矩阵)

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. 【题解】SHOI2008仙人掌图

    本质上还是树形dp.建立圆方树,遇到圆点的时候直接求(和树形dp一样即可),遇到方点做中转点的时候要考虑会从圆的另一侧通过(需满足最短路径的原则).原本是对于圆上的点进行 \(n^{2}\) 的匹配, ...