poj 1159 Palindrome lcs 滚动数组
来源:互联网 发布:网络统考成绩查询短信 编辑:程序博客网 时间:2024/06/04 17:55
题意:求长度为n的字符串最少需要添加几个字符可以使之成为回文串。
思路:求原串和反串的最长公共子序列。然后总长减去最大的lcs的值即可。
用int 内存占用约95M, 题目要求是64M显然超内存, 所以用short
int 。 还能用滚动数组。
#include<iostream>#include <cstdio>#include <cstring>#include<vector>using namespace std;const int MAXN=5005;char s1[MAXN], s2[MAXN];short int dp[MAXN][MAXN];short int Max(short int a, short int b){ if(a>b) return a; return b;}int main(){ int n; while(~scanf("%d", &n)) { scanf("%s", s1+1); memset(dp, 0, sizeof(dp)); int len = strlen(s1+1); for(int i=1;i<=len;++i) s2[i]=s1[len-i+1]; s2[len+1]='\0'; for(int i=1;i<=len;i++) { for(int j=1;j<=len;++j) { if(s1[i]==s2[j]) dp[i][j]=Max(dp[i-1][j-1]+1, dp[i][j]); else dp[i][j]=Max(dp[i][j-1], dp[i-1][j]); } } printf("%d\n", len-dp[len][len]); } return 0;}
滚动数组
每两行求一个lcs, 往下递推。
#include<iostream>#include <cstdio>#include <cstring>#include<vector>using namespace std;const int MAXN=5005;char s1[MAXN], s2[MAXN];short int dp[2][MAXN];short int Max(short int a, short int b){ if(a>b) return a; return b;}int main(){ int n; while(~scanf("%d", &n)) { scanf("%s", s1+1); memset(dp, 0, sizeof(dp)); int len = strlen(s1+1); for(int i=1;i<=len;++i) s2[i]=s1[len-i+1]; unsigned short int MAX=0; s2[len+1]='\0'; for(int i=1;i<=len;i++) { for(int j=1;j<=len;++j) { if(s1[i]==s2[j]) dp[i%2][j]=Max(dp[(i-1)%2][(j-1)]+1, dp[i%2][j]); else dp[i%2][j]=Max(dp[i%2][j-1], dp[(i-1)%2][j]); //MAX=Max(dp[i%2][j],MAX); } } printf("%d\n", len-dp[len%2][len]); } return 0;}
阅读全文
0 0
- 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+滚动数组】【水题】
- poj 1159 Palindrome lcs 滚动数组
- POJ 1159 Palindrome(lcs加滚动数组)
- poj 1159 Palindrome (DP:LCS+滚动数组)
- poj 1159 Palindrome LCS一维滚动数组优化
- HDU1513 Palindrome(LCS+滚动数组)
- Palindrome POJ1159 LCS+滚动数组
- POJ 1159 Palindrome(滚动数组)
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 1159 Palindrome (LCS)
- Palindrome POJ 1159 【LCS】
- 框架的种子模块
- 算法--简单选择排序
- 并发技术_4_CyclicBarrier
- linux命令之history,update
- Robot Framework 接口传入实时Cookie
- poj 1159 Palindrome lcs 滚动数组
- 关于C#的委托和事件详细解析
- left()函数、right()函数与substr()函数
- Vue2.0---将页面中表格数据导出excel (详细)
- springmvc 学习笔记和配置
- 条款03:尽可能使用const
- C++研发-找工作知识点
- 歌曲音乐爬虫编写
- 方法的反射