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. 阿里云服务器 linux下载 jdk

    直接从本地下载包上传比较慢.直接在服务器上下载安装包: 1.进入orcle官网; 2.选择需要下载的版本,下载需要同意orcle协议, 3.点击下载,获取到下载请求的cookie, 复制所有cooki ...

  2. 漫谈单点登录(SSO)

    1. 摘要 ( 注意:请仔细看下摘要,留心此文是否是您的菜,若浪费宝贵时间,深感歉意!!!) SSO这一概念由来已久,网络上对应不同场景的成熟SSO解决方案比比皆是,从简单到复杂,各式各样应有尽有!开 ...

  3. PAT 1065 单身狗

    https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...

  4. 算法(2) Find All Numbers Disappeared in an Array

    题目:整数数组满足1<=a[i]<=n(n是数组的长度),某些元素出现一次,某些元素出现两次,在数组a[i]中找到[1,n]区间中未出现的数字.比如输入[4,3,2,7,8,2,3,1], ...

  5. could not read column value from result set:

    错误描述: INFO [http-apr-8080-exec-26] (NullableType.java:203) - could not read column value from result ...

  6. WebSocket添加事件监听器(6)

    WebSocket编程遵循异步编程模型;打开socket后,只需要等待事件发生,而不需要主动向服务器轮询,所以需要在WebSocket对象中添加回调函数来监听事件. WebSocket对象有三个事件: ...

  7. BZOJ4651 NOI2016网格(割点)

    首先显然可以通过孤立角落里的跳蚤使其不连通,所以只要有解答案就不会大于2.同样显然的一点是当且仅当跳蚤数量<=2且连通时无解.做法其实也很显然了:特判无解,若跳蚤不连通输出0,否则看图中是否无割 ...

  8. BZOJ4423 AMPPZ2013Bytehattan(并查集)

    判断网格图中某两点是否被割开,可以将割边视为边区域视为点,转化为可切割这两点的区域是否连通.于是每次判断使两个区域连通后是否会形成环(边界视为连通),若是则说明被两点被割开.并查集维护. #inclu ...

  9. P2483 【模板】k短路([SDOI2010]魔法猪学院)

    题目背景 感谢@kczno1 @X_o_r 提供hack数据 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界 ...

  10. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...