POJ 1159 (转换为求LCS)
来源:互联网 发布:linux mint 内核版本 编辑:程序博客网 时间:2024/05/17 23:05
题意:给定一个字符串,任意位置添加字母形成回文字串,最少添加几个字母.
思路:把字符串反转,求反转串和原串的最长公共子序列,ans=n-lcs
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX_N = 5005;char s[MAX_N], t[MAX_N];int dp[3][MAX_N + 1];int main(){ int n; while(cin >> n) { cin >> s; memset(dp, 0, sizeof(dp)); for (int i = n - 1, j = 0; i >= 0; --i) { t[i] = s[j++]; } int e = 0; for(int i = 0; i < n; ++i) { e = 1 - e; for(int j = 0; j < n; ++j) { if(s[i] == t[j]) { dp[e][j + 1] = dp[1 - e][j] + 1; } else { dp[e][j + 1] = max(dp[1 - e][j + 1], dp[e][j]); } } } cout << n - dp[e][n] << endl; } return 0;}
0 0
- POJ 1159 (转换为求LCS)
- uva10635(LCS转换为求LIS)
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 1159 LCS
- poj 1159 Palindrome (LCS)
- Palindrome POJ 1159 【LCS】
- poj 1159 (LCS)
- POJ-1159-Palindrome-lcs扩展
- POJ 1159 Palindrome (LCS)
- POJ 1159 Palindrome (LCS)
- poj 2250 Compromise dp求lcs+输出方案
- UVA 10635 - Prince and Princess ( LCS 转换为LIS )
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
- 巧妙转换为求逆序数-状态数组(POJ-3067)
- POJ 1159 LCS 黑书练习题
- POJ 1159 - Palindrome 优化空间LCS
- poj 1159 Palindrome 【LCS + 滚动数组】
- Java后端2017书单推荐
- ViewPropertyAnimator概述
- 安卓蓝牙开发
- Java 程序员 面试前必备知识
- jsp前台传值到action后台的方法
- POJ 1159 (转换为求LCS)
- NS-3安装总结
- 0002算法--------整数划分问题算法分析及JAVA代码完美实现
- DB2的常用命令
- 每日一句:Light and Darkness
- 九、内容提供者详解
- 论教学型教材和手册型教材
- redis read time out 解决
- 关于height,offsetHeight,clientheight,innerWidth,offsetTop简单解释