Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 60290   Accepted: 20998

Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

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

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

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:动态规划的更多相关文章

  1. Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)

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

  2. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  3. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  4. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  5. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  6. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  7. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  8. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  9. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

随机推荐

  1. Ubuntu: 无法使用su命令

    Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...

  2. OC_NSFileManager

    案例1:通过文件扩展名进行搜索文件  NSFileManager *manager = [NSFileManager defaultManager]; /* 在Mac OS X系统有一个代表主目录的速 ...

  3. 数据库系统概论学习3-SQL 语句和关系代数(一)SQL 入门

    3. SQL 语句和关系代数(一)SQL 入门 3.1 数据库的编程语言 SQL 的优点 SQL 集成了数据查询(data query).数据操作(data manipulation).数据定义(da ...

  4. 模型融合之blending和stacking

    1. blending 需要得到各个模型结果集的权重,然后再线性组合. """Kaggle competition: Predicting a Biological Re ...

  5. Android LCD(二):常用接口原理篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/9125799 关键词:Android LCD TFT TTL(RGB)  LVDS  E ...

  6. 百度竞价推广URL通配符使用说明

    {keywordid} 被替换为触发该创意的关键词ID(全局唯一ID,不是字面ID),当没有对应的keywordid时,替换为0. {creative} 被替换为所点击的创意ID(全局唯一ID). 2 ...

  7. EntityFramework 学习 一 Colored Entity in Entity Framework 5.0

    You can change the color of an entity in the designer so that it would be easy to see related groups ...

  8. Android 手机上获取手机当前上网IP地址

      [转] 原文              Android 手机上获取手机当前上网IP地址                (手机网关给手机号分配的IP) 每个手机上网通过移动网关的时候,网关都会给该手 ...

  9. ajax02-XMLHttpRequest 对象的使用

    XMLHttpRequest 是 AJAX 的基础,用于在后台与服务器交换数据.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. XMLHttpRequest 对象 所有现代浏览器均 ...

  10. 在移动端做查看日志信息的js

    Vconsole.js下载这个js引入需要使用的html文件,当运行到页面是就会在右下角显示提示,点击就出出现像pc端上的控制台页面 在线查看的网址 http://console.hongliang. ...