滚动数组 poj1159 Palindrome
来源:互联网 发布:ios移动网络dns被劫持 编辑:程序博客网 时间:2024/05/17 04:57
题意:在一个字符串中插入一些字符,使字符串变成回文串,问插入的最小数量
...原来还有这种逆天公式
把这个字符串,和翻转后的字符串去算最长公共子序列,设为x
如果原长为L,那么答案就是L-x
仔细想想,还很有道理的样子....
因为,除了那部分公共子序列以外,只要把反序列的剩下的插入到正序里面去,就会形成回文串了。。
然后这题数据比较大,,但是好在求最长公共子序列可以用滚动数组解决
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<functional>#include<algorithm>using namespace std;typedef long long LL;typedef pair<int, int> PII;const int MX = 5500 + 5;const int INF = 0x3f3f3f3f;char S1[MX], S2[MX];int dp[2][MX];int main() { int L; while(~scanf("%d%s", &L, S1 + 1)) { for(int i = 1; i <= L; i++) { S2[i] = S1[L - i + 1]; } int cur = 0, nxt = 1; for(int i = 1; i <= L; i++) { memset(dp[nxt], 0, sizeof(dp[nxt])); for(int j = 1; j <= L; j++) { if(S1[i] == S2[j]) { dp[nxt][j] = dp[cur][j - 1] + 1; } else { dp[nxt][j] = max(dp[nxt][j - 1], dp[cur][j]); } } swap(cur, nxt); } printf("%d\n", L - dp[cur][L]); } return 0;}
0 0
- 滚动数组 poj1159 Palindrome
- Palindrome POJ1159 LCS+滚动数组
- poj1159 palindrome(动态规划+滚动数组)
- POJ1159 Palindrome 动态规划+滚动数组
- POJ1159 Palindrome(滚动数组DP)
- poj1159 Palindrome 最长公共子序列 + 滚动数组
- poj1159 Palindrome(最长公共子序列, dp+滚动数组)
- poj1159 dp 滚动数组
- poj1159(滚动数组 + DP)
- poj1159-LCS滚动数组优化。
- POJ1159——Palindrome——DP+滚动数组(节省空间)
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
- POJ1159 Palindrome
- poj1159 - Palindrome
- POJ1159 Palindrome
- POJ1159--Palindrome
- POJ1159 Palindrome
- poj1159 Palindrome
- 《Effective Java》——异常
- 设计模式_桥梁模式
- 几个面试好地址
- hdu 5313 Bipartite Graph 贪心+bfs染色
- The broken pedometer-纯暴力枚举
- 滚动数组 poj1159 Palindrome
- c语言编程的时间函数详解
- 使用 C# 开发智能手机软件:推箱子(十二)
- TCP/IP详解学习笔记--UDP协议
- 数据结构:树
- Mac Android Studio SVN
- 使用 C# 开发智能手机软件:推箱子(十四)
- 海贼王之伟大航路解题报告
- java中的关键字super