给一字符串,问最少加几个字符能够让它成为回文串。

比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd



思路:

动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几个字符,枚举子串长。

if str[i] == str[j]:

DP[i][j] = DP[i + 1][j - 1]

else:

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



注意:

长度较大为 5000,二维数组 5000 * 5000 须要将类型改为 short,

不需考虑 j - i < 2 的特殊情况,

由于矩阵的左下三角形会将DP[i + 1][j - 1]自己主动填零,

可是用滚动数组的时候须要考虑 j - i < 2。用滚动数组的时候,空间会变为 3 * 5000,

可这时候 DP 的含义略微变下。

DP[i][j] 意味着从第 j 个字符右移 i 个长度的字符串变为回文串所须要的最少字符数目。

3.也能够用 LCS 的方法,字符串长 - LCS( 串。逆串 ) 

#include <iostream>
#include <cstring>
using namespace std; int nLen;
char str[5005];
short DP[5005][5005]; int main(){ memset( DP, 0, sizeof( DP ) );
cin >> nLen;
cin >> str; for( int len = 1; len < nLen; ++len ){
for( int start = 0; start + len < nLen; ++start ){
int end = start + len;
if( str[start] == str[end] )
DP[start][end] = DP[start + 1][end - 1];
else
DP[start][end] = min( DP[start + 1][end], DP[start][end - 1] ) + 1;
}
}
cout << DP[0][nLen - 1];
return 0;
}

滚动数组 715K:

#include <iostream>
#include <cstring>
using namespace std; int nLen;
char str[5005];
short DP[3][5005]; int main(){ memset( DP, 0, sizeof( DP ) );
cin >> nLen;
cin >> str; for( int len = 1; len < nLen; ++len ){
for( int start = 0; start + len < nLen; ++start ){
int end = start + len;
if( str[start] == str[end] && ( end - start >= 2 ) )
DP[len % 3][start] = DP[( len - 2 ) % 3][start + 1];
else if( str[start] != str[end] )
DP[len % 3][start] = min( DP[( len - 1 ) % 3][start + 1],
DP[( len - 1 ) % 3][start] ) + 1;
}
}
cout << DP[(nLen - 1) % 3][0];
return 0;
}

动态规划+滚动数组 -- POJ 1159 Palindrome的更多相关文章

  1. LCS(滚动数组) POJ 1159 Palindrome

    题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度 ...

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

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

  3. 2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组)

    2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组) https://www.luogu.com.cn/problem/P2516 题意: 给定字符串 \(S\) ...

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

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

  5. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

  6. HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)

    题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...

  7. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  8. POJ_1159 Palindrome (线性动态规划+滚动数组)

    题意是说,给定一个字符串,问至少还需要插入多少个字符才能使得该字符串成为回文字符串. 这道题一开始做的时候用了一个简单的动态规划,开了一个5000*5000的数组,用递归形式实现,代码如下: 其中d[ ...

  9. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

随机推荐

  1. merge into Oracle里的 saveOrUapdate

    1.初始数据: SQL> select * from a; ID NAME ---------------------- ---------------------- 1 1 2 1 3 1 4 ...

  2. Python django admin 替换表单控件

    测试版本: Python 2.7 Django 1.6.2 models.py from django.db import models class Article(models.Model): ti ...

  3. Selenium 下载URL

    http://mvnrepository.com/artifact/org.seleniumhq.selenium

  4. C# 中使用 OpenSSL 的公钥/私钥进行加密和解密

    在C#中进行RSA解密,需要用RSACryptoServiceProvider,但是不支持OpenSSL格式的公钥或者私钥. X509 公钥 -----BEGIN PUBLIC KEY----- MI ...

  5. yii2源码学习笔记(二)

    yii\base\Object代码详解 <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 200 ...

  6. highcharts实例教程一:结合php与mysql生成折线图

    Highcharts是一款纯javascript和html5编写的图表库,不仅几乎能兼容所有pc浏览器,而且对ios和android手机端的兼容 性也不错,它能够很简单便捷的在Web网站或Web应用中 ...

  7. Python自动化运维之12、面向对象进阶

    上一篇<面向对象基础>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...

  8. UVA 11722 几何概型

    第六周A题 - 几何概型 Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descriptio ...

  9. ASP.NET MVC的Action Filter

    一年前写了一篇短文ASP.NET MVC Action Filters,整理了Action Filter方面的资源,本篇文章详细的描述Action Filter.Action Filter作为一个可以 ...

  10. linux下tomcat配置APR方式HTTPS

    一.安装APR 创建/usr/local/apr tar zxvf apr-1.4.5.tar.gz cd apr-1.4.5 ./configure --prefix=/usr/local/apr/ ...