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. XJOI网上同步训练DAY2 T1

    [问题描述] 为了迎接校庆月亮中学操场开始施工.不久后操场下发现了很多古墓这些古墓中有很多宝藏.然而学生们逐渐发现自从操场施工之后学校的运气就开始变得特别不好.后来经过调查发现古墓下有一个太守坟由于操 ...

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

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

  3. 自定义JSP标签实现语言国际化(类似struts text标签),并同时支持图片、JS文件国际化

    源代码及样例下载地址: http://download.csdn.net/detail/u014569459/7169385 一.功能说明: 1. 支持语言国际化 2. 支持图片文件.JS文件国际化 ...

  4. linux中的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的 ...

  5. ZooKeeper架构设计及其应用

    ZooKeeper是一个开源的分布式服务框架,它是Apache Hadoop项目的一个子项目,主要用来解决分布式应用场景中存在的一些问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置管理等 ...

  6. qt model/view 架构基础介绍之QListWidget

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  7. 多线程下不反复读取SQL Server 表的数据

    在进行一些如发送短信.邮件的业务时,我们常常会使用一个表来存储待发送的数据,由后台多个线程不断的从表中读取待发送的数据进行发送.发送完毕后再将数据转移到历史表中,这样保证待发送表的数据普通情况下不会太 ...

  8. 《TCP/IP具体解释卷2:实现》笔记--4种不同类型的mbuf

    mbuf的主要用途是保存子进程和网络接口间互相传递的用户数据.但mbuf也用于保存其它各种数据:源于目的地址.插口 选项等等. 以下介绍我们要遇到的四种类型的mbuf,它们根据在成员m_flag中填写 ...

  9. [Regular Expressions] Find a String that Precedes Another String ?= , ?!

    Let's image tow cases for the following string: var str = `foo foobar foobaz fooboo` First of all: w ...

  10. apache访问控制设置

    apache访问控制设置 (2009-03-17 11:24:36) 转载▼ 标签: it 杂谈   Order allow,deny    默认情况下禁止所有客户机访问 Order deny,all ...