Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 58168   Accepted: 20180

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
最少插入多少个字符使得原来的字符串变成回文串DP[i][j]表示长度为i的第j个字符开头的字串需要插入的个数
状态转移方程:(字符串下表以1开始)
  dp[i][j]=dp[i-2][j+1]  如果s[i+j-1]==s[j]
  dp[i][j]=min(dp[i-1][j-1],dp[i-1][j+1])+1 如果不等
 #include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 5001
short int dp[Max][Max];
char s[Max];
int main()
{
int len;
int i,j;
freopen("in.txt","r",stdin);
scanf("%d",&len);
scanf("%s",s);
memset(dp,,sizeof(dp));
for(i=;i<=len;i++)
{
for(j=;j<=len-i;j++)
{ if(s[i+j-]==s[j])
dp[i][j]=dp[i-][j+];
else
dp[i][j]=min(dp[i-][j],dp[i-][j+])+;
}
}
printf("%d\n",dp[len][]);
return ;
}

Palindrome(POJ 1159 DP)的更多相关文章

  1. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

  2. Sumsets(POJ 2229 DP)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 15293   Accepted: 6073 Descrip ...

  3. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  4. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  5. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

  6. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  7. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  8. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. dp乱写1:状态压缩dp(状压dp)炮兵阵地

    https://www.luogu.org/problem/show?pid=2704 题意: 炮兵在地图上的摆放位子只能在平地('P') 炮兵可以攻击上下左右各两格的格子: 而高原('H')上炮兵能 ...

随机推荐

  1. 函数:灵活即强大 - 零基础入门学习Python018

    函数:灵活即强大 让编程改变世界 Change the world by program 上节课我们基本介绍了Python函数的用法,非常简单.这节课我们主要针对函数的参数和返回值进一步深入学习.学习 ...

  2. sqlserver2008 case when then else end

    在写sql语句的时候,有时候我们需要对字段的只进行判断计算.查看了下相关文档在sql2012.sql2014里边可以使用  IIF 函数直接完成. 但是现在我需要在sql2008版本中完成最字段的计算 ...

  3. Windows 8.1 正式版镜像下载大全

    该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. [声明:所有资源均来自于网络,请购买正版授权后再使用.]Win8.1 正式版原版镜像 ...

  4. 自己实现的简单MVC框架(类似Struts2+Spring)

    一.框架简介 本框架是一个类似于Struts2+Spring的框架,目的在于个人钻研和技术分享,将流行技术框架Struts2.Spring中使用到的主要技术以较为简化的方式实现出来,给大家一个更直观的 ...

  5. exe可执行程序及堆栈分配(转载)

    可执行程序的内存分布 GNU编译器生成的目标文件默认格式为elf(executive linked file)格式,这是Linux系统所采用的可执行链接文件的通用文件格式.elf格式由若干个段(sec ...

  6. Zigzag Iterator 解答

    Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  7. yii2学习的论坛

    http://www.yiifans.com/forum.php http://www.yiichina.com/ http://www.yiichina.com/

  8. 自定义一个searchBar

    #import "CZSearchBar.h" @implementation CZSearchBar - (instancetype)initWithFrame:(CGRect) ...

  9. IOS Main函数

    int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSS ...

  10. Android FragmentActivity onActivityResult冲突问题

    场景:假设fragment的填充数据是ListView,ListView中的每一项都有button,点击每一项的button跳转到B界面.在B界面处理完业务需返回fragment中更新数据. 解决方式 ...