题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串)

分析: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(回文串) 动态规划的更多相关文章

  1. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  2. poj 1159 dp回文串

    题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  3. 洛谷T89644 palindrome回文串

    洛谷 T89643 回文串(并查集) 洛谷:https://www.luogu.org/problem/T89643 题目描述 由于 Kiana 实在是太忙了,所以今天的题里面没有 Kiana. 有一 ...

  4. 1159 Palindrome(最小插入回文串)

    标题效果 定的字符串长度的串和内容.中的字符可以在任何位置被插入.它至少需要为数字,这使得编程回文串串. 回文序列从左至右,从右到左和读取相同. 例如. aaaacbbbb它是一个回文串 aaab前面 ...

  5. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. (回文串 )Best Reward -- hdu -- 3613

    http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    ...

  8. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  9. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

随机推荐

  1. RM-Linux驱动--Watch Dog Timer(看门狗)驱动分析

    from:http://blog.csdn.net/geekcome/article/details/6595265 硬件平台:FL2440 内核版本:2.6.28 主机平台:Ubuntu 11,04 ...

  2. linux进程间通信--无名管道

    管道 只能用于具有亲缘关系的进程之间通信是一个半双工的通信模式, 具有固定的写读端和写端,管道可以看成一种特殊的文件,对它可以使用普通的read.write等操作 管道的创建: #include &l ...

  3. c#简单的Json解析类

    使用方法: 引用Newtonsoft.Json.dll文件,然后引用命名空间using Newtonsoft.Json.Linq;JsonDome中有实例,照做就行 现在贴上示例代码 using Ne ...

  4. MySql中常用语句

    1.查询语句: SELECT  查询字段  FROM  表名   WHERE 条件 查询字段可以使用 通配符* 字段名 别名(把长的名字命名一个别名,比较短的) 通配符:SELECT * FROM ' ...

  5. [简历] JAVA 软件工程师

    首先,一份好的简历不光说明事实,更通过FAB模式来增强其说服力. Feature:是什么 Advantage:比别人好在哪些地方 Benefit:如果雇佣你,招聘方会得到什么好处 其次,写简历和写议论 ...

  6. 推荐一款不错的GIF录制软件附带.NET源码

    源码下载地址:http://www.51aspx.com/Code/ScreenToGif10

  7. 第 5 章 工厂方法模式【Factory Method Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了.这个故事是说 ...

  8. [CC150] Find a line passing the most number of points

    Problem: Given a two-dimensional graph with points on it, find a line which passes the most number o ...

  9. C#中数据库连接的配置文件

    在C#2010中,如何保存和访问数据库的连接字符串呢? 在Winform下要新增App.config文件,在Asp.net下要新增web.config文件. 1.打开配置文件添加相关代码后如下即可: ...

  10. 剖析ECMALL的登录机制

    在ecmall.php文件中实例化控制器类,每一个控制器类,必须继承(extends)upload\admin\app\backend.base.php文件.在继承中调用方法是谁先被继承谁的方法被先调 ...