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. Node 抓取非utf-8编码页面

    代码示例 Nodejs抓取非utf8字符编码的页面 -- Ruby's Louvre var http = require('http'); var iconv = require('iconv-li ...

  2. U-Boot 不能识别FAT32 SD分区

    /********************************************************************* * U-Boot 不能识别FAT32 SD分区 * 说明: ...

  3. Git详解之七 自定义Git

    以下内容转载自:http://www.open-open.com/lib/view/open1328070404827.html自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式, ...

  4. 如何让Beamer的logo放在右上角

    # 位置需要导入包```\usepackage{beamerfoils}\usepackage{tikz}\usepackage{pgf}\MyLogo{%%\includegraphics[heig ...

  5. 【Python爬虫学习笔记(2)】正则表达式(re模块)相关知识点总结

    1. 正则表达式 正则表达式是可以匹配文本片段的模式. 1.1 通配符 正则表达式能够匹配对于一个的字符串,可以使用特殊字符创建这类模式.(图片来自cnblogs) 1.2 特殊字符的转义 由于在正则 ...

  6. autoconf / automake工具使用介绍

    本文转自:http://blog.csdn.net/gulansheng/article/details/42683809 一.简介 作为Linux下的程序开发人员,一定都遇到过Makefile,用m ...

  7. apply函数应用

    (1)找到数组中最小或最大的数字 var v = [1,23,4,9]; console.log(Math.min.apply(Math,v));

  8. erl_0018 erlang_看门狗001_“内存大量占用检测及解决办法”

    绪:erlang出现问题百分之六七十在于内存问题,“进程消息队列爆炸”.“进程堆栈持续增长” 工具:erlang:memory(),erlang:system_info(process_count). ...

  9. POJ3686 The Windy's 【费用流】*

    POJ3686 The Windy’s Description The Windy’s is a world famous toy factory that owns M top-class work ...

  10. 一个查看Cookie的便捷工具——EditThisCookie

    Appium正在努力准备中,很快就要和大家见面了- 今天给大家分享一个查看cookies的工具,用fiddler总感觉有点麻烦,还乱七八糟的找不到到底哪个链接是当前网站的cookies: 首先,你用的 ...