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. 关于SharePoint 的Client object model该何时load和execut query的一点自己的看法

    很多人在用client object model的时候,不知道何时或者该不该load,今天看到一个观点描述这个问题,觉得很有道理,和大家分享.那就是写client object model就像写sql ...

  2. ArcGis在Oracle中常用的sql

    ) m 查询坐标数据 更新坐标: update tableName set SHAPE = mdsys.sdo_geometry(2002,28420,null,mdsys.sdo_elem_info ...

  3. IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)

    目录 IP协议首部格式地址解析协议 ARP逆向地址解析协议 RARP网际控制报文协议 ICMP网际组管理协议IGMP IP 数据报首部 IP数据报首部格式: 最高位在左边,记为0 bit:最低位在右边 ...

  4. MyBatis入门(六)---mybatis与spring的整合

    一.整合需要 1.1.方法 上一章中的数据 需要spring通过单例方式管理SqlSessionFactory spring和mybatis整合生成代理对象,使用SqlSessionFactory创建 ...

  5. iOS开发笔记11:表单键盘遮挡、浮点数价格格式化显示、省市区选择器、View Debugging

    1.表单键盘遮挡 应用场景为一个collectionView上有多个textfield.textView供用户填写信息. 之前输入项较少时,采取的方法比较粗暴,didSelectItemAtIndex ...

  6. MySQL初步笔记,有待整理

     查询表纪录: select * from tb1; 插入一条记录 insert tb1 values(value1,value2,...); 修改表的默认编码: alter table tb1 ch ...

  7. 数据仓库建模与ETL实践技巧

    数据分析系统的总体架构分为四个部分 —— 源系统.数据仓库.多维数据库.客户端(图一:pic1.bmp) 其中,数据仓库(DW)起到了数据大集中的作用.通过数据抽取,把数据从源系统源源不断地抽取出来, ...

  8. TreeSize工具介绍

    TreeSize Professional 工具是一个功能强大且灵活方便的硬盘空间管理工具,能在 Windows 8/7/Vista/XP 或 Windows Server 2012年/2008年/2 ...

  9. Google Guava之--cache

    一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...

  10. inux下文件权限设置中的数字表示权限,比如777,677等,这个根据什么得来的

    chmod ABC file 其中A.B.C各为一个数字,分别表示User.Group.及Other的权限. A.B.C这三个数字如果各自转换成由“0”.“1”组成的二进制数,则二进制数的每一位分别代 ...