ACM POJ 1159 Palindrome 回文词
来源:互联网 发布:nba数据库统计三分 编辑:程序博客网 时间:2024/06/05 23:26
题目大意:输入一个字符串,确定往该字符串中添加最少的字符数,从而将该字符串变为回文字符串。
算法思想:
用a[] b[] 分别存储字符串的正序和到序,之后求出a[]和b[]的最长公共子序列,然后用字符串的长度减去最长公共子序列的长度就OK了。
代码如下:
#include <iostream>#include <cstring>#include <climits>#include <algorithm>using namespace std;const int H=5050;char a[H];//正序char b[H];//逆序int c[3][H]; //记录子序列的长度最大值 此处用到了滚动数组 如果开串c[h][h]会超内存(MLE),或改为 short int c[H][H]int LCS(int m,int n){//求出最长公共子序列的长度 for(int i=0;i<=2;i++){ c[i][0]=0; } for(int j=0;j<=n;j++){ c[0][j]=0; } for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(a[i]==b[j]) c[i%2][j]=c[(i-1)%2][j-1]+1; else{ c[i%2][j]=max(c[(i-1)%2][j],c[i%2][j-1]); } } } return c[m%2][n];}int main(){ int n; while(cin>>n){ for(int i=1;i<=n;i++){ cin>>a[i]; b[n-i+1]=a[i]; } cout<<n-LCS(n,n)<<endl; } return 0;}
0 0
- ACM POJ 1159 Palindrome 回文词
- POJ 1159--Palindrome(回文序列)
- POJ-1159-Palindrome-回文-动态规划
- poj 1159 Palindrome(最长回文子串)
- POJ 1159 Palindrome(字符串变回文:LCS)
- POJ 1159 Palindrome(LCS~~回文)
- poj 1159 Palindrome -- 回文串,动态规划
- POJ 3974 Palindrome 回文串
- poj 3974 Palindrome 最长回文
- poj 1153 Palindrome(回文字符串)
- 【转】POJ 1159 Palindrome【经典的DP回文问题】
- POJ 1159 Palindrome(动态规划:最长回文子序列)
- POJ 1159-Palindrome(dp_回文串+滚动数组)
- POJ 1159 Palindrome(最长回文子序列+滚动数组)
- POJ 1159 Palindrome(回文串,最长公共子序列)
- palindrome (回文词)
- HDU 3068 最长回文 / POJ 3974 Palindrome
- POJ 4617 Palindrome(最长回文子串)
- SQL Server 2008 -Performance counter registry failed 解决办法
- 冒泡排序的三种实现方法
- 数据库 整理
- Power Designer 使用
- 陈天:如何快速掌握一门技术?
- ACM POJ 1159 Palindrome 回文词
- linux系统编程-学习笔记4-chmod/access/chown/link/truncate
- 香奈儿专柜最新款女包上市 真皮小羊皮经典菱格纹镶嵌chanel金属牌 一件代发
- 为什么要构造内核源码树?编写驱动时必须简历内核树吗?
- Incorrect line ending: found carriage return (\r) without corresponding newline (\n)的解决方法
- Android坐标的简单方法介绍
- 解决SVN造成的桌面图标问号
- 语法——原型prototype深入理解
- Remove Duplicates from Sorted List II