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

比方 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. C++ Built-In Array 的语义

    C++ 编译花了大量精力使得class和原始类(primitive types)的用法一致.比如array的应用: A a[100]:// A is class int b[100]: 虽然a是用户定 ...

  2. 全志A10_linux3.0内核编译记录

    第一次尝试编译时按照 Android A10(4.0.3)系统编译详解.pdf 编译打包出来的镜像可用,但是内核没有生成Module.symvers .后来进一步研究才知道 ./build.sh 脚本 ...

  3. TCP/IP-TCP

    Don't cry over spilt milk. "覆水难收" 参考资料:TCP/IP入门经典 (第五版)  TCP/IP详解 卷一:协议 TCP是协议栈中非常重要的一个部分, ...

  4. PHP添加、更新solr索引

    <?php $options = array ( 'hostname' => 'localhost', 'port' => '8080', 'path'=>'solr/help ...

  5. C# 构造函数如何调用父类构造函数或其他构造函数

    class C : B{    C() : base(5)      // call base constructor B(5)    {  }    C(int i) : this()  // ca ...

  6. 扩展PHP内置的异常处理类

    在try代码块中,需要使用throw语句抛出一个异常对象,才能跳到转到catch代码块中执行,并在catch代码块中捕获并使用这个异常类的对象.虽然在PHP中提供的内置异常处理类Exception,已 ...

  7. 随机数是骗人的,.Net、Java、C为我作证(转载)

      几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生成一个数,我们事先也不知道它生成什么数.比如在.Net中编写下面的代码: Random rand = ...

  8. ServiceStack.OrmLite

    ServiceStack.OrmLite 谈谈我的入门级实体框架Loogn.OrmLite   每次看到有新的ORM的时候,我总会留意一下,因为自己也写过一个这样的框架,人总是有比较之心的.我可能会d ...

  9. NOIP[2015] 运输计划

    传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...

  10. Spring <context:annotation-config/>

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ...