Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 62102   Accepted: 21643

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

本题是一个求LCS长度的问题,具体算法及思路可以参考

http://blog.csdn.net/v_july_v/article/details/6695482

注意用int会爆空间

#include<iostream>
using namespace std; short c[5005][5005];
char s[5005]; int main()
{
int n;
cin>>n; for(int i=1;i<=n;i++)
{
cin>>s[i];
} for(int i=0;i<=n;i++)
c[i][0]=c[0][i]=0; for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(s[i]==s[n-j+1])
c[i][j]=c[i-1][j-1]+1;
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
else
c[i][j]=c[i][j-1];
} cout<<n-c[n][n]<<endl; return 0;
}

  

[POJ] Palindrome的更多相关文章

  1. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  2. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. OpenJudge/Poj 1159 Palindrome

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

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

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

  5. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  6. poj 1159 Palindrome - 动态规划

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

  7. 【POJ 1159】Palindrome

    [POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...

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

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

  9. poj 3280 Cheapest Palindrome

    链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...

随机推荐

  1. select2切换事件如何生效

    1.问题背景 利用select2生成可搜索下拉框,并且绑定切换事件:但是直接绑定change事件,发现不起作用 2.问题原因 <!DOCTYPE html> <html> &l ...

  2. Objective C - 4 - 下载图片并且加载到View

    #import "LoadInternetImageViewController.h" @interface LoadInternetImageViewController () ...

  3. libudev-dev : Depends: libudev0 (= 175-0ubuntu9) but 175-0ubuntu9.3 is to be installed 错误解决方案

    libudev-dev : Depends: libudev0 (= -0ubuntu9) but -0ubuntu9. is to be installed 错误解决方案 参考文章: 1. ubun ...

  4. php simple_html_dom

    这个真的很好用,如果用正则,就太麻烦了. 首先,下载simple_html_dom,用include_once就可以使用了. 可以直接定位,可以像个对象一样操作,很方便. $ret=file_get_ ...

  5. 《selenium2 python 自动化测试实战》(13)——上传文件

    看代码: # coding: utf-8 from selenium import webdriver from time import sleep driver = webdriver.Firefo ...

  6. java 并发时使用条件变量--Condition

    lock--unlock的方式在实际中使用较少,一般使用synchronized获取对象的内部锁替代,但是lock--unlock对了解synchronized有很大的帮助. 创建一个bank对象用于 ...

  7. sql server2008升级

    安装了试用版的sql server2008,要升级为正式企业版.仅仅须要使用安装程序的 维护-升级 功能.在升级时输入企业版序列号,就能升级为正式版.以下给两个序列号 开发版: PTTFM-X467G ...

  8. FastAdmin env.sample 的用法

    FastAdmin env.sample 的用法 在 FastAdmin 的 1.0.0.20180513 中我提交了一个 PR,增加 env.sample 内容如下: [app] debug = f ...

  9. 【转】电信100M光纤无线下载速度仅为5MB/秒的困惑

    原文网址:http://itbbs.pconline.com.cn/50463999.html 在江苏电信官方测速网站测速的.1.光猫F460有线连接至笔记本,下载速度为12MB/秒左右:2.F460 ...

  10. 【常见Web应用安全问题】---4、Directory traversal

    Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些.  常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Si ...