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

设原序列S的逆序列为S’ ,这道题目的关键在于,

最少需要补充的字母数 = 原序列S的长度 — S和S’的最长公共子串长度

题意:

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

做法:

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

那么a[i],b[i]的最长公共子序列就是所求的字符串里拥有的最大的回文串。(我不知道这个结论是怎么来的,求大牛评论)

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

求最长公共子序列的公式为:

dp[i][j]=max(dp[i-1] [j],dp[i][j-1])

if(a[i]==b[i])

dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);

分析:简单做法是直接对它和它的逆序串求最长公共子序列长度len。n-len即为所求。(n为原串长度)

这样做的原因如下:

要求最少添加几个字符,我们可以先从原串中找到一个最长回文串,然后对于原串中不属于这个回文串的字符,在它关于回文串中心的对称位置添加一个相同字符即可。那么需要添加的字符数量即为n-最长回文串长度。

最长回文串可以看作是原串中前面和后面字符的一种匹配(每个后面的字符在前面找到一个符合位置要求的与它相同的字符)。这种的回文匹配和原串与逆序串的公共子序列是一一对应的(一个回文匹配对应一个公共子序列,反之亦然),而且两者所涉及到的原串中的字符数量是相等的,也就是最长公共子序列对应最长回文串。原因陈述完毕。

还有另一个动态规划的方法。

f[i][j]表示从i到j这段子串若变为回文串最少添加的字符数。

if (st[i] == st[j])

f[i][j] = f[i + 1][j - 1];

else

f[i][j] = min(f[i + 1][j], f[i][j - 1]) + 1;

本来是想用滚动数组做的,可是发现自己并不懂原理,就先没用了,看网上说数组开成short int 可以过。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(a,b) (a>b?a:b)
using namespace std;
short int s[5001][5001];
int main(){
int a[5001];
int b[5001];
char str;
int n;
cin>>n;
getchar();
for(int i=1,j=n;i<=n;i++,j--){
scanf("%c",&str);
a[i]=str;
b[j]=str;
}
for(int i=0;i<=n;i++){
s[0][i]=0;
s[i][0]=0;
}
// memset(s,0,sizeof(s));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
s[i][j]=max(s[i][j-1],s[i-1][j]);
if(a[i]==b[j])
s[i][j]=max(s[i][j],s[i-1][j-1]+1);
}
}
int len;
len = s[n][n];
printf("%d\n",n-len);
return 0;
}

POJ 1159 Palindrome 最长公共子序列的问题的更多相关文章

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

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

  2. POJ 1159:Palindrome 最长公共子序列

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56273   Accepted: 19455 Desc ...

  3. POJ1159——Palindrome(最长公共子序列+滚动数组)

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

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

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48526   Accepted: 16674 Description A p ...

  5. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  6. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. 周赛F题 POJ 1458(最长公共子序列)

    F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Description ...

  8. hdu 1159求最长公共子序列

    题目描述:给出两个字符串,求两个字符串的公共子序列(不是公共子串,不要求连续,但要符合在原字符串中的顺序) in: abcfbc abfcab programming contest abcd mnp ...

  9. HDU 1159 LCS最长公共子序列

    #include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...

随机推荐

  1. Error Domain=com.google.greenhouse Code=-102

    *** Terminating app due to uncaught exception 'com.google.greenhouse', reason: 'Error Domain=com.goo ...

  2. 【转载】介绍“Razor”— ASP.NET的一个新视图引擎

    最近在做一个项目,用的MVC razor 视图,因为之前没用这个视图做过,于是查阅文档资料,共享一下. https://msdn.microsoft.com/zh-cn/ff849693 内容主要是讲 ...

  3. CSS3 文本装饰

    浏览器对CSS3文本特性的支持情况,如下表所示: 浏览器 text-shadow text-overflow word-wrap hyphens Opera 9.5+ 9+.带前缀-o- 10.5+ ...

  4. boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  5. 去掉Visual Studio 编辑器里中文注释的红色波浪线 转载

    我们通常用visual studio进行开发的时候,我们通常会用到一款比较流行比较方便的插件,那就是Visual Assist X,它可以增强Microsoft开发环境下的编辑能力,支持C/C++,C ...

  6. java设计模式——接口模式

    java将接口的概念提升为独立的结构,体现了接口与实现分离.java接口允许多个类提供相同的功能,也允许一个同时实现多个接口.java的接口与抽象类十分相似.java与抽象类中的区别: 1.一个类可以 ...

  7. Action配置

    Action是一个逻辑控制器,并不直接对浏览器生成响应,而是返回指定逻辑视图(一个字符串). 不推荐在Action的name属性值中使用点(.)和中划线(-),有可能会引发一些未知异常.   1使用A ...

  8. JavaScript学习总结【7】、JS RegExp

    1.RegExp 简介 RegExp 即正则表达式(Regular Expression,在代码中常简写为 regex.regexp或RE/re/reg),就是使用单个字符串来描述.匹配一系列符合某个 ...

  9. Ubuntu 在右键快捷菜单中添加“Open in Terminal”

    操作步骤翻译如下: 1.打开一个Terminal(ctrl+alt+t),输入如下指令 sudo apt-get install nautilus-open-terminal 2.使用以下指令来重启N ...

  10. Python 学习笔记(3) - 控制流、函数

    控制流语句if.while.for.break.continue以上从最终作用效果来讲,同学过的其他语言没有什么不同.需要注意的只是语法,而Python 在语法上是如此让人赞叹和喜欢啊. 控制流语句的 ...