POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description
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
Output
Sample Input
5
Ab3bd
Sample Output
2
题意:给出n,表示接下去给出的字符串长度为n,求最少插入几个字符可以使该字符串变成回文字串。
思路:设原字符串为a,反转该字符串设为b,用字符串长度减去a、b的最长公共子序列即可。
注意:
- 若用%c输入,记得getchar
- 即使利用了最长公共子序列中后,dp数组开[5050][5050]会造成内存超限,这时候需要利用滚动数组进行处理(结合奇偶性进行重复利用),由于我们只需要求出dp[n][n],所以前面部分的空间其实是可以重复利用的,比如说1->2存完之后,我们需要存3了,但是没有必要再去开辟空间,因为前面1所占的空间之后已经不需要再用了,所以我们可以将3存到1的位置,即3->1,以此类推 4->2。dp只需开到[2][5050]即可,即对每一部分i%2。
- 顺带注意区别一下子序列和字串,子序列是不要求满足连续性的,而字串是需要连续的。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; char a[],b[];
int dp[][];
int main()
{
int n;
cin>>n;
memset(dp,,sizeof(dp));
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b)); scanf("%s",a); int p=;
for(int i=n-; i>=; i--)
b[p++]=a[i]; for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
if(a[i]==b[j])
dp[(i+)%][j+]=dp[i%][j]+;
else
dp[(i+)%][j+]=max(dp[i%][j+],dp[(i+)%][j]);
}
}
cout<<n-dp[n%][p]<<endl;
return ;
}
POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)的更多相关文章
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- POJ 1159:Palindrome 最长公共子序列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56273 Accepted: 19455 Desc ...
- POJ1159——Palindrome(最长公共子序列+滚动数组)
Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...
- Palindrome(最长公共子序列)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 48526 Accepted: 16674 Description A p ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53414 Accepted: 18449 Desc ...
- 周赛F题 POJ 1458(最长公共子序列)
F - F Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description ...
随机推荐
- Vlan的相关知识点收纳
Q.思科Vlan的上限个数是多少? VLAN的范围:根据平台和软件版本不同,Cisco交换机最多支持4096个VLAN.VLAN号共有4096个,0-4095 0,4095:这两个号保留,仅限系 ...
- SDOI2017 树点染色
\[SDOI2017 树点染色\] 题目描述 Bob 有一棵 $ n $ 个点的有根树,其中 $ 1 $ 号点是根节点.Bob 在每个节点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是,这 ...
- robotframework 时间控件的操作的几种方法总结。
- 使用linkedhashmap实现LRU(最近最少使用缓存算法)
import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...
- mac 安装brew mac安装expect mac一键登录服务器脚本
mac 安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- 3-Windows-CMD启动mysql服务-连接本地mysql服务-连接远程mysql服务
转自: https://jingyan.baidu.com/article/84b4f565b77a5660f6da32d4.html 备注: 如果在连接远程mysql服务,无法连接时,可能是远程my ...
- Match & Catch CodeForces - 427D 后缀自动机水题
题意: 给出两个字符串a,b,求一个字符串,这个字符串是a和b的子串, 且只在a,b中出现一次,要求输出这个字符串的最小长度. 题解: 将a串放入后缀自动机中,然后记录一下每个节点对应的子串出现的次数 ...
- 【gcc】更新下载编译gcc遇到的各种问题
帮学长的oj升级gcc版本.遇到了贼多问题.. [悲惨的开始] 安装gcc版本推荐ustc的mirror的下载,超快der... https://mirrors.ustc.edu.cn/gnu/gcc ...
- yes - 不断输出一个字符串,直到杀死其为止
SYNOPSIS(总览) yes [OPTION]... [STRING]... DESCRIPTION(描述) 不断输出包括所有指定STRING(s)的一行,或者是`y'. --help 显示帮助并 ...
- 深入研究js中的位运算及用法
什么是位运算? 位运算是在数字底层(即表示数字的 32 个数位)进行运算的.由于位运算是低级的运算操作,所以速度往往也是最快的(相对其它运算如加减乘除来说),并且借助位运算有时我们还能实现更简单的程序 ...