POJ 1159 - Palindrome 优化空间LCS
来源:互联网 发布:ubuntu访问共享文件夹 编辑:程序博客网 时间:2024/05/14 23:45
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文..
问题转化为求LCS..但是n最大到5000...裸的LCS需要dp[5000][5000]..显然会爆空间.
.而更新的时候之于上一层的数据有关...所以空间不需要开5000*5000...滚动数组..只需2*5000就好...
Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<set>#include<algorithm>#include<cmath>#define oo 1000000007#define ll long long#define pi acos(-1.0)#define MAXN 10005using namespace std;int n,dp[2][5005];char s1[MAXN],s2[MAXN];int main(){ //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); int i,j,k; while (~scanf("%d",&n)) { scanf("%s",s1+1); for (i=1;i<=n;i++) s2[i]=s1[n-i+1]; k=0; for (i=1;i<=n;i++) { k=1-k; for (j=1;j<=n;j++) if (s1[i]==s2[j]) dp[k][j]=max(max(dp[1-k][j-1]+1,dp[1-k][j]),dp[k][j-1]); else dp[k][j]=max(dp[k][j-1],dp[1-k][j]); } printf("%d\n",n-dp[k][n]); } return 0;}
- POJ 1159 - Palindrome 优化空间LCS
- POJ - 1159 - Palindrome (LCS + 优化)
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 1159 Palindrome (LCS)
- Palindrome POJ 1159 【LCS】
- poj 1159 Palindrome LCS一维滚动数组优化
- POJ-1159-Palindrome-lcs扩展
- POJ 1159 Palindrome (LCS)
- POJ 1159 Palindrome (LCS)
- poj 1159 Palindrome 【LCS + 滚动数组】
- poj 1159 Palindrome --- LCS减内存
- hdu1513&&poj 1159 Palindrome(LCS+滚动数组)
- poj 1159 Palindrome lcs+滚动数组
- POJ 1159 Palindrome(字符串变回文:LCS)
- POJ 1159-Palindrome(DP/LCS变形)
- POJ 1159 Palindrome(LCS~~回文)
- poj 1159 Palindrome(lcs类似dp)
- MS SQL Server 2005数据库可疑、置疑修复方法
- 触摸事件和手势
- NDK学习1
- VC6.0连接错误
- linux中stdout,stdin,stderr
- POJ 1159 - Palindrome 优化空间LCS
- MS SQL Server MSDB等系统数据库损坏的修复
- Ubuntu-server 10.04.4查看无线网卡型号
- ilmerge工具合并多个DLL或EXE
- SQL Server CONVERT() 函数
- 如何恢复linux的环境变量ubuntu中root用户没有办法使用命令
- mfc 得到exe绝对路径
- 访问 IIS 元数据库失败解决方法
- SQL Server 2005 在只有MDF文件的情况下恢复(丢失日志文件)