POJ 1159 Palindrome
2015-10-20 01:21
274 查看
L(longest)C(common)S(sequence)经典问题,要用滚动数组,不然会爆内存。
#include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; int dp[2][5005]; int main() { string s1, s2; int n; while (cin >> n) { cin >> s1; s2 = s1; reverse(s2.begin(), s2.end()); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { // notice the order dp[i % 2][j] = dp[(i - 1) % 2][j] > dp[i % 2][j - 1] ? dp[(i - 1) % 2][j] : dp[i % 2][j - 1]; if (s1[i - 1] == s2[j - 1]) { int temp = dp[(i - 1) % 2][j - 1] + 1; dp[i % 2][j] = dp[i % 2][j] > temp ? dp[i % 2][j] : temp; } } } cout << n - dp[n % 2] << "\n"; } return 0; }
相关文章推荐
- Swift基础篇——结构体
- Ember.js 入门指南——查询记录
- Swift基础篇——单例
- Swift基础篇——懒加载
- 使用Maven构建Dubbo服务的可运行jar包
- Unity中HideInInspector和SerializeField
- LeetCode OJ 123 Best Time to Buy and Sell Stock III
- Swift基础篇——闭包
- 类加载器ClassLoader之jar包隔离
- 用软件工程分析开源项目octave的移植
- VS的代码分析工具
- Swift基础篇——setter和getter的使用
- 查找算法--折半查找
- 图形库与消息循环
- Swift基础篇——构造函数
- 每天学点儿树莓派(三)——使用IDE提高效率
- 不改源码的方式实现osgearth当中placenode的中文支持
- 买一个密码只需55美分,网络安全真是浮云?
- Swift基础篇——函数
- 正则学习(1)--- 基本规则总结