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. 多个互相有联系的checkbox的单选逻辑

    checkbox单选的状态逻辑,状态好的时候一下就写好了,状态不好的时候要调试比较久,当然主要是对其中的事件不太清楚. 先给出效果图吧. 然后给出代码, selectZhiFuBaoPay.setOn ...

  2. Linq转成sql后的分页方法

    sql 分页 -- Region Parametersdeclare @pageindex int set @pageindex=2set @pagesize=10 DECLARE @p0 Int = ...

  3. My97 DatePicker 选择时间后弹出选择的时间

    项目中用到这个时间插件,注册用户时可以选中永久和选择时间,二者是互斥关系, 所以在选择时间插件时,需要绑定一个事件,所以看到了这个插件: <input id="yydate" ...

  4. 基于shiro授权过程

    1.对subject进行授权,调用方法isPermitted("permission串")2.SecurityManager执行授权,通过ModularRealmAuthorize ...

  5. CoreAnimation

    CoreAnimation 1.CABasicAnimation // position CABasicAnimation *ba = [CABasicAnimation animationWithK ...

  6. ios专题 - openSSL

    1  概述 1.1  产生背景 基 于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐.由于这些应用都需要在网络上进行在线交易,它们对网络通信的安全 性提出了更高的要求 ...

  7. 原生与jqueryDOM

    总结与复习原生与jquery的DOM操作. 获取元素节点: $(".class") $("#id") $(".class div") $(& ...

  8. Sizzle引擎执行的流程图

    Sizzle有太多都不是太懂,但能看懂这张图. 图片来源: http://www.cnblogs.com/aaronjs/p/3332805.html

  9. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  10. 学渣也要搞 laravel(1)—— 安装篇

    看到laravel(我叫它:拉瓦)那么热门,我也决定学上一学. Laravel 5.2 在 5.1 基础上继续改进和优化,添加了许多新的功能特性:多认证驱动支持.隐式模型绑定.简化Eloquent 全 ...