Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 51631   Accepted: 17768

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

题意:

给你一串字符串,让你求最少增加几个字符,才干使得这个字符串是个回文串。

分析:

设a[i]是这个字符串,b[i]是这个字符串的逆序串。

那么a[i],b[i]的最长公共子序列就是所求的字符串里拥有的最大的回文串。

然后用总串长度减去最大的回文串长度即为所求。

求最长公共子序列:

1).递归式写成:

2).回溯输出最长公共子序列过程:

#include<stdio.h>
#include<iostream>
using namespace std;
#define Max 5001 char a[Max],b[Max];
short int dp[Max][Max]; // 用short int数组 int max(int x,int y)
{
return (x>y?x:y);
} int main ()
{ int n,i,j;
scanf("%d",&n);
getchar();
for(i=1,j=n;i<=n,j>=1;i++,j--)
{
scanf("%c",&a[i]);
b[j]=a[i];
} for(i=0;i<=n;i++)
{
dp[i][0]=0;
dp[0][i]=0;
} for(i=1;i<=n;i++) // 求最长公共子序列
{
for(j=1;j<=n;j++)
{
if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
} }
printf("%d\n",n-dp[n][n]); // 总串长度减去最长公共子序列(最大的回文串)长度
return 0;
}

poj 1156 Palindrome的更多相关文章

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

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

  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

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

  8. POJ 1159 - Palindrome (LCS, 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 Desc ...

  9. poj 1159 Palindrome(dp)

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

随机推荐

  1. 解决TextView排版混乱或者自动换行的问题

    其实在TextView中遇到排版自动换行而导致混乱不堪的情况是非常常见的,而且导致这种问题产生的原因就是英文和中文混合输入,半角字符和全角字符混合在一起了.一般情况下,我们输入的数字.字母以及英文标点 ...

  2. Android开发FAQ集锦!!!

    .Android SDK应该从什么地方下载?为什么(http://developer.Android.com/ )经常上不上去? 答:谷歌官网的 (http://developer.Android.c ...

  3. Windows Embedded Compact 7 开发环境搭建

    第一步,我们会定制一个运行在Vitual PC上的image.要完成这个任务,你要保证你的电脑上安装了:1.Windows Virtual PC 2.Visual Studio 2008+SP1 3. ...

  4. 安全:Web 安全学习笔记

    背景 说来惭愧,6 年的 web 编程生涯,一直没有真正系统的学习 web 安全知识(认证和授权除外),这个月看了一本<Web 安全设计之道>,书中的内容多是从微软官方文档翻译而来,这本书 ...

  5. .NET:“事务、并发、并发问题、事务隔离级别、锁”小议,重点介绍:“事务隔离级别"如何影响 “锁”?

    备注 我们知道事务的重要性,我们同样知道系统会出现并发,而且,一直在准求高并发,但是多数新手(包括我自己)经常忽略并发问题(更新丢失.脏读.不可重复读.幻读),如何应对并发问题呢?和线程并发控制一样, ...

  6. unity3d摄像机参数

    1. Clear Flags:清除标记.决定屏幕的哪部分将被清除.一般用户使用对台摄像机来描绘不同游戏对象的情况,有3中模式选择: Skybox:天空盒.默认模式.在屏幕中的空白部分将显示当前摄像机的 ...

  7. Tomcat 下 Memcached 集群与 Terracotta 集群比较

    总结:Terracotta 集群配置要比Memcached 集群简单,但Terracotta 集群启动的速度要比Memcached 集群慢,性能Terracotta 集群要比Memcached 集群好 ...

  8. go omitempty 忽略类型

    nil false 0 每个结构字段的编码可以通过结构字段标签中“json”键下存储的格式字符串来定制.格式字符串给出字段的名称,可能后跟逗号分隔的选项列表.名称可能为空,以指定选项而不覆盖默认字段名 ...

  9. LinkedHashMap源码剖析

    首先还是类似的,我们写一个简单的LinkedHashMap的程序: public class Test { public static void main(String[] args) { Map&l ...

  10. C/C++ 语言获取文件大小

    在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明 ...