POJ1159:动态规划
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 60290 | Accepted: 20998 |
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
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=;
unsigned short int dp[MAXN][MAXN];//dp[i][j]表示以第i个字母开头,第j个字母结尾的构成回文子串所需添加的字符串
char s[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
for(int i=;i<n;i++)
{
dp[i][i]=;//有一个字符时
}
for(int i=;i<n;i++)
{
if(s[i-]==s[i])
dp[i-][i]=;
else
dp[i-][i]=;
}
for(int k=;k<n;k++)
{
for(int i=,j=k;j<n;i++,j++)
{
if(s[i]==s[j])
dp[i][j]=dp[i+][j-];
else
dp[i][j]=min(dp[i+][j],dp[i][j-])+;
}
}
printf("%d\n",dp[][n-]);
}
return ;
}
POJ1159:动态规划的更多相关文章
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
随机推荐
- MySQL数据库基本操作(一)
进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2 Sample Outpu ...
- 修改redhat默认显示语言为中文
[delmore@localhost Desktop]$ su //切换到最高权限 Password: ...
- js,java,ajax实现跨域访问及其原理
http://blog.csdn.net/saytime/article/details/51540876 这篇文章对跨域访问做了较为细致得分析,我这里做下简单总结 1.实现跨域访问原理: 浏览器由于 ...
- SpringCloud-路由网关(Zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.只能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统. 在Spring Cloud ...
- vue中编辑代码是不注意格式时会报错
1.是因为我们使用了eslint的代码规范,我们不要使用这种规范就好 2.在build目录下找到webpack.base.conf.js 在里面找到关于eslint的相关配置注释或移除掉就好
- rem根据网页的根元素(html)来设置字体大小
rem根据网页的根元素来设置字体大小,和em(font size of the element)的区别是,em是根据其父元素的字体大小来设置,而rem是根据网页的跟元素(html)来设置字体大小
- 【转载】JAVA多线程读取、操作List集合
本文转载自:http://blog.csdn.net/wang1989cs/article/details/47663565 import java.util.ArrayList; import ja ...
- 【转载】postgreSQL在linux中安装详解
.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgresq ...
- python 矩阵转置transpose
在读图片时,会用到这么的一段代码: image_vector_len = np.prod(image_size)#总元素大小,3*55*47 img = Image.open(path) ...