hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串)
分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解
状态转化方程:
f[x][y]=0 初始化
f[x][y]=f[x+1][y-1] s[x]==s[y]时
f[x][y]=MIN ( f[x+1][y] , f[x][y-1] )+1 s[x] != s[y]时
代码展示
一开始没有将算的的数据存放到数组中,使得有些数据重复计算好多次,TLE 。 f[x][y]可以记录值
空间复杂度挺高的,f数组仅仅用到一半的存储空间,可以通过x与y计算,将二维数组转换成一维数组 f[(max+1)*max/2]
// hdu 1159 Palindrome 49392K 1797MS
#include<iostream>
#include<cstdio>
#define MIN(a,b) ((a)>(b)?(b):(a))
using namespace std;
char s[];
short f[][];//存放区间内插入最小字符个数
int result(int be, int fi)
{
if(f[be][fi]!=-)return f[be][fi]; //已求出最小值
else if(be>=fi)return ; //代表结束
else if(s[be] == s[fi]) return f[be][fi] = result(be+,fi-);
else
{
int t1 = result(be,fi-);
int t2 = result(be+,fi);
return f[be][fi]=MIN(t1,t2)+;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
cin >> s[i];
memset(f,-,sizeof(f));
cout << result(,n)<<endl;
}
return ;
}
经典典型动态规划代码(没有重复计算)每次计算用到的数据都是前面计算得到的结果,没有冗余
对于计算结果f[x][y] 由于计算结果总是与f[x+1][y-1]、f[x+1][y]、f[x][y-1]三个变量有关联 因而 x 从后往前开始循环,y从x向后开始循环
// hdu 1159 Palindrome 40708K 344MS
#include<iostream>
#include<cstdio>
#define MIN(a,b) ((a)>(b)?(b):(a))
using namespace std;
char s[];
short f[][];
int main()
{
int n,i,j;
while(cin >> n >> s+)
{
for(i=n; i>0; i--)
for(j=i+1; j<=n; j++)
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;
cout << f[][n] << endl;
}
return ;
}
hdu 1159 Palindrome(回文串) 动态规划的更多相关文章
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- poj 1159 dp回文串
题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- 洛谷T89644 palindrome回文串
洛谷 T89643 回文串(并查集) 洛谷:https://www.luogu.org/problem/T89643 题目描述 由于 Kiana 实在是太忙了,所以今天的题里面没有 Kiana. 有一 ...
- 1159 Palindrome(最小插入回文串)
标题效果 定的字符串长度的串和内容.中的字符可以在任何位置被插入.它至少需要为数字,这使得编程回文串串. 回文序列从左至右,从右到左和读取相同. 例如. aaaacbbbb它是一个回文串 aaab前面 ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- (回文串 )Best Reward -- hdu -- 3613
http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) ...
- 集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
随机推荐
- u-boot和linux的机器码
先看u-boot的机器码和linux的机器码是在什么地方决定的. 1. u-boot的机器码是在u-boot的board/fs2410/fs2410.c文件里决定的: /* arch numb ...
- CentOS系统安全配置
http://down.51cto.com/data/318797 http://www.centos.bz/2011/07/centos-system-security-configure/ htt ...
- Yum安装Memcache
rpm -qa | grep libevent yum install libevent -y rpm -qa | grep memcached yum install memcached ...
- NotePad++更改背景颜色
白色的编辑框看得眼睛不舒服,怎么样更改NotePad++的背景颜色使眼睛更舒服些? 1.设置--语言格式设置 2.设置背景色 “背景色”一栏,选择背景色颜色 “使用全局背景色”一栏要打上√,否则无 ...
- JAVA NIO之Character Set
明白以下几个概念: 字母集(Character Set),汉字,特殊符号,字母这些都是字符集: 字符编码集(Coded character set),将字符集的字符使用数字进行编码:比如ASCII,就 ...
- iOS中使用RegexKitLite来试用正则表达式 使用ARC 20个错误解决办法
You can also disable the ARC for the RegexKitLite only by adding a flag: select the project -> YO ...
- capitalize()在Python中含义
Python为string对象提供了转换大小写的方法:upper() 和 lower(). 还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法, 以及所有单词首字 ...
- QLGame 2D Engine编写环境搭建
QLGame 2D Engine编写 (win7环境搭建) 广州麒麟网络工作室,计划制作一款2d game engine,基于opengl(es)平台,暂时支持android,以后考虑支持linux, ...
- linux 监控
http://www.iyunv.com/thread-50606-1-1.html http://segmentfault.com/a/1190000002537665 http://blog.cs ...
- 【BZOJ 2693】jzptab(莫比乌斯+分块)
2693: jzptab Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的结果 Sample I ...