线性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,插入了之后就相当于抵消了,所以就和在 ...
随机推荐
- 爬虫实战--利用Scrapy爬取知乎用户信息
思路: 主要逻辑图:
- VC连接access
(1)首先拷贝 c:\program files\common files\system\ado\ 目录中的 msado15.dll 文件到项目中. (2)在VC中加入DLL,具体方法如下: (3)创 ...
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
暂时可以通过 在 scope template 中自己处理格式化解决 相关issue: 2548
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- 【Tomcat】tomcat设置http文件下载,配置文件下载目录
tomcat作为http的下载服务器,网上有很多办法 但我认为最简单的是:(亲测有效) 1.直接把文件放在 /var/lib/tomcat6/webapps/ROOT 目录下, 2.然后在网址中访问: ...
- nfs挂载无法卸载
故障现象:今天发现服务器的upload负载很高,到18左右,同时df查看磁盘命令卡住 用top\vmstat\iostat查看并未发现可以服务或进程. 上网查发现可能是nfs问题. 卸载nfs挂载的方 ...
- Firefox缓存文件夹位置设置及清除缓存方法
地址栏敲入: about:config, 新建一个"browser.cache.disk.parent_directory", 并设置为你要的缓存文件夹, 例如: "F ...
- centos 6.5配置ftp服务器,亲测可用
设置开机启动 1 chkconfig vsftpd on 启动服务 1 /sbin/service vsftpd start 配置FTP用户组/用户以及相应权限 添加用户组 1 groupadd ft ...
- Zabbix3.0 API调用
Zabbix API 是什么? API简单来说是服务对外开放的一个接口,用户通过该接口传递请求,完成操作.API的背后是一组方法的集合,这些方法实现了服务对应的不同功能,调用API实际上就是换了一种方 ...
- 庆祝团队合著的《自主实现SDN虚拟网络与企业私有云》终于得以出版 --- 本人负责分布式存储部分的编写
https://item.jd.com/12154254.html 京东购买地址