POJ 1159 Palindrome(LCS)
题目链接:http://poj.org/problem?id=1159
题目大意:给定一串字符,添加最少的字符,使之成为回文串。
Sample Input
5
Ab3bd
Sample Output
2 分析:这道题目之前有做过,就是将原字符串逆序得到另一个字符串,求它们的最长公共子序列,这样就能求得它的可以构成回文的最长字符数,用n减去即为添加最少可使之成为回文的数目。 可恨的是之前一直超内存,看了别人的解题报告,原来定义dp[MAX][MAX]时,不用int型,而是short型,内存只占int的一半(见上一篇日志) 另外逆序字符串可以不用新开一个数组,也可以直接在原数组上从后往前循环。 代码如下:
# include<stdio.h>
# include<string.h> #define MAX 5005 int max(int a,int b,int c){
int temp;
temp = a>b ? a :b;
return temp>c ?temp :c ;
}
char s[MAX],t[MAX];
short dp[MAX][MAX]; //定义的类型为short int main(){
int i,j,n;
while(scanf("%d",&n)!=EOF){
scanf("%s",s);
for(i=;i<n;i++)
t[i] = s[n-i-];
t[n] = ;
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
for(j=;j<=n;j++){
if(s[i-] == t[j-])
dp[i][j] = dp[i-][j-] + ;
dp[i][j] = max(dp[i][j],dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",n-dp[n][n]);
}
return ;
}
另一种方法:dp[i][j]表示从第i个字符到第j个字符能构成的回文添加的最少字符。
#include <stdio.h>
#define Min(a,b) (a<b?a:b)
const int MAX = ;
char ch[MAX];
short dp[MAX][MAX]={};
int main()
{
int lenth, i, j;
while(scanf("%d%s",&lenth, ch+)!=EOF)
{
for(i=lenth;i>;i--) //自底向上
{
for(j=i+;j<=lenth;j++)
{
if(ch[i]==ch[j]){
dp[i][j] = dp[i+][j-];
}
else{
dp[i][j] = Min(dp[i+][j],dp[i][j-])+;
}
}
}
printf("%d\n",dp[][lenth]);
}
return ;
}
可是在某些变态的OJ里,上面的方法仍然不能AC,这是需要引入滚动数组优化空间,比如在第一种方法之上
代码如下:
# include<stdio.h>
# include<string.h> #define MAX 5005 int max(int a,int b,int c){
int temp;
temp = a>b ? a :b;
return temp>c ?temp :c ;
}
char s[MAX],t[MAX];
short dp[2][MAX]; int main(){
int i,j,n;
while(scanf("%d",&n)!=EOF){
scanf("%s",s);
for(i=;i<n;i++)
t[i] = s[n-i-];
t[n] = ;
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
for(j=;j<=n;j++){
if(s[i-] == t[j-])
dp[i%][j] = dp[(i-)%][j-] + ;
dp[i%][j] = max(dp[i%][j],dp[(i-)%][j],dp[i%][j-]);
}
}
printf("%d\n",n-dp[n%][n]);
}
return ;
}
POJ 1159 Palindrome(LCS)的更多相关文章
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- POJ 1159 Palindrome(最长公共子序列)
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...
- POJ - 1159 Palindrome(dp-回文变形)
d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...
- poj 1159 Palindrome 【LCS】
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...
- poj 1080 基因组(LCS)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19376 Accepted: ...
- HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- POJ题目分类(转)
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- ubuntu 手动添加jar到本地仓库
前言:maven仓库的下载速度太慢了,而且有些jar不存在,或者加入自己开发的依赖包,还是要学会自己手动加入jar.下面以 javax.servlet-api为例. 1.获取下载的javax.serv ...
- HDU 4737 A Bit Fun 2013成都 网络赛 1010
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4737 题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i, ...
- fopen/fclose
在操作文件之前要用fopen打开文件,操作完毕要用fclose关闭文件; 打开文件就是在操作系统中分配一些资源用于保存该文件的状态信息,并得到该文件的标示,以后用户程序就可以这个标志对文件做各种操作了 ...
- OSI 七层模型和 TCP/IP 协议比较
OSI (Open System Interconnection), 开放式系统互联参考模型.从下到上七层模型功能及其代表协议: 物理层(Physical) :规定了激活.维持.关闭通信端点之间的 ...
- hdoj 3665 Seaside【最短路】
Seaside Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 395. Coins in a Line II
最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...
- 超强vim配置文件
简易安装方法: https://github.com/ma6174/vim 打开终端,执行下面的命令就自动安装好了: wget -qO- https://raw.github.com/ma6174/v ...
- java数据结构之hash表
转自:http://www.cnblogs.com/dolphin0520/archive/2012/09/28/2700000.html Hash表也称散列表,也有直接译作哈希表,Hash表是一种特 ...
- 通过rest接口获取自增id (twitter snowflake算法)
1. 算法介绍 参考 http://www.lanindex.com/twitter-snowflake%EF%BC%8C64%E4%BD%8D%E8%87%AA%E5%A2%9Eid%E7%AE% ...
- [TypeScript] Configuring a New TypeScript Project
This lesson walks you through creating your first .tsconfig configuration file which will tell the T ...