线性DP POJ 1159 Palindrome
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 59101 | Accepted: 20532 |
Description
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
Output
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的更多相关文章
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- poj 1159 Palindrome(区间dp)
题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- poj 1159 Palindrome(dp)
题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...
- OpenJudge/Poj 1159 Palindrome
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...
- POJ 1159 Palindrome(LCS)
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
随机推荐
- 一个JAVA渣渣的校招成长记,附BAT美团网易等20家面经总结
欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享美文,分享各种Java学习资源,面试题,以及企业级Java实战项目回复关键字免费领取): 今天分享一篇牛客网上的一个 ...
- 用C#实现对MSSqlServer数据库的增删改查---Server层(WaterLevelSetServer.cs、DeviceSetServer.cs)
在Server层定义WaterLevelSetServer和WaterLevelRecordServer两个子类,分别继承DeviceSetServer和DeviceRecordServer. usi ...
- windows下制作debian U盘启动
制作平台:Windows 7 制作debian版本:debian 7.4 wheezy 1.下载引导镜像,包含三个文件:boot.img.gz(解压备用).initrd.gz 和 vmlinuz. h ...
- python模块之xlrd,xlwt,读写execl(xls,xlsx)
安装xlrd,xlwt pip install xlrd xlwt xlrd读取execl [环境ipython python2.7.5] import xlrd book = xlrd.open_w ...
- 大型网站的 HTTPS 实践(二)——HTTPS 对性能的影响(转)
原文链接:http://op.baidu.com/2015/04/https-s01a02/ 1 前言 HTTPS 在保护用户隐私,防止流量劫持方面发挥着非常关键的作用,但与此同时,HTTPS 也会降 ...
- HEVC代码记录(删除)
得到编码残差 TEncSearch.cpp 4543:rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
- 简易web-slide
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 初涉yield
function* a(i) { console.log('here we go'); yield i; // 必须有*,不然b会作为返回值,而不是执行 yield* b(i); yield i+10 ...
- 458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- [MySQL] specified key was too long max key length is 767bytes
https://blog.csdn.net/u012099869/article/details/53815084/