Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 59094   Accepted: 20528

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

Source

 
题意:
给一串字符,问最少加多少个字符可以使它变成回文字符串。
(回文字符串=从左往右看 和 从右往左看 串相同)
 
short数组直接过
/* f[i][j]表示从字符标号i-j 变成回文串需要的最少步数
不难得出
若s[j]==s[i+j] : f[j][i+j]=f[j+1][i+j-1],
否则 : f[j][i+j]=min(f[j+1][i+j],f[j][i+j-1])+1
*/
#include<cstdio>
#include<iostream>
using namespace std;
#define N 5010
char s[N];int n;
short int f[N][N];
int main(){
scanf("%d%s",&n,s);
for(int i=;i<=n;i++){
f[i][i]=;f[i][i+]=s[i]==s[i+]?:;//初始化
}
for(int i=;i<=n;i++){//循环两个字母的间隔
for(int j=;i+j<n;j++){//循环开端字母
if(s[j]==s[i+j]) f[j][i+j]=f[j+][i+j-];
else f[j][i+j]=min(f[j+][i+j],f[j][i+j-])+;
}
}
printf("%d\n",f[][n-]);
return ;
}

poj 1159 Palindrome的更多相关文章

  1. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  2. OpenJudge/Poj 1159 Palindrome

    1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...

  3. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  4. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  5. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  6. poj 1159 Palindrome(区间dp)

    题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...

  7. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  8. POJ 1159 Palindrome(最长公共子序列)

    http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...

  9. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

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

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

随机推荐

  1. cl_gui_cfw=>dispatch

    将已经触发的EVENT发送给他们各自的EVENT HANDLER,以便让这些事件得到响应. 根据返回值可以判断是否发送成功. CALL METHOD cl_gui_cfw=>dispatch   ...

  2. SharePoint 2013 使用PowerShell创建State Service

    今天,搞SPD配置的sp2010wf迁移到sp2013环境上去,发布解决方案都很正常,给列表添加wf的时候报错“该表单无法显示,可能是由于 Microsoft SharePoint Server St ...

  3. 读书笔记2014第6本:《The Hunger Games》

    以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空间中看到Mockingjay,说是不错的英文小说,好像已经是 ...

  4. Python基础(2)--对象类型

    Python使用对象模型来存储数据.构造任何类型的值都是一个对象 所有的Python对象都拥有三个特性:身份.类型.值 身份: 每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以使用内建函数i ...

  5. OpenGL ES学习笔记(三)——纹理

    首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <OpenGL ES学习笔记( ...

  6. linux---文本编辑vi

    本文摘自:鸟哥的linux私房菜

  7. 重要选择器querySelector和querySelectorAll

    他们的作用是根据 CSS 选择器规范,便捷定位文档中指定元素. 目前几乎主流浏览器均支持了他们.包括 IE8(含) 以上版本. Firefox. Chrome.Safari.Opera. queryS ...

  8. CentOS下如何完全卸载MySQL?卸载自带的mysql

    CentOS下如何完全卸载MySQL?解决卸载不干净的问题 系统:CentOS 6.5,MySQL:MySql 5.6 这里主要解决使用rpm安装的卸载问题,安装方法见:CentOS安装mysql*. ...

  9. INFORMATICA 的部署实施 MTP&MTS

    软件开发的一般都有三个环境,开发环境,用户接受度测试环境,生产环境.我最近实施了从开发环境到生产环境的部署工作,在此跟大家分享一下. 大概步骤如下: 1 备份生产环境INFORMATICA 知识库  ...

  10. D_S 循环队列的基本操作

    //  main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...