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

比方 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. 数据库连接未关闭,conn与rs未关闭

    场景: 2000多人使用系统,早上打卡签到,时间点比较集中. 程序:会创建connction连接.但是未关闭,导致tomcat挂了.导致连接池已满 解决:conn.close,rs.close.记住一 ...

  2. 多核CPU利用测试

      一直在想程序上是否特意让线程在指定的CPU上去运行,这样可以提高运行效率,所以特地写个代码让CPU使用率画正弦曲线的实验,我使用的是AMD X4 641的CPU,为四核四线程的片子. 代码如下 # ...

  3. 随机数(random)

    在测试你的程序是否超时时,可以随机生成一组大数据,进行一下测试. 当然如果你考场上一道题直接读不懂不会做的时候,可以random一下,拼一下RP嘛.2333. #include<cstdio&g ...

  4. json-lib 使用教程

    //关于java map与JSONObject类互相转换 Map<String,Object> map=new HashMap<String,Object>(); map.pu ...

  5. node.js使用经验记录

    MongoDB使用经验: 有时不知道MongoDB的错误码代表什么,那有这个链接: https://github.com/mongodb/mongo/blob/master/src/mongo/bas ...

  6. 运行Capture.exe找不到cdn_sfl401as.dll

    今天运行capture Orcad16.6显示缺少cdn_sfl401as.dll,昨天运行时并没有发现这种情况,回想今天安装了modelsim之后才发生这种情况,于是将modelsim卸载掉,再次启 ...

  7. 转:视觉中国的NoSQL之路:从MySQL到MongoDB

    起因 视觉中国网站(www.chinavisual.com)是国内最大的创意人群的专业网站.2009年以前,同很多公司一样,我们的CMS和社区产品都构建于PHP+Nginx+MySQL之上:MySQL ...

  8. 常用的JQuery UI框架

    http://www.ligerui.com/ http://www.jeasyui.com/index.php http://www.jqwidgets.com/

  9. poj Pie

    http://poj.org/problem?id=3122 #include<cstdio> #include<cstring> #include<cmath> ...

  10. 关于Qt在子线程中使用QMessageBox的折衷方法

    Qt将所有GUI相关的处理都限制在主线程中,这么做有助于防止意想不到的访问冲突产生,但也限制了线程中某些简单的UI交互的实现,比如QMessageBox. 因为QMessageBox必须在主线程中打开 ...