题目链接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)的更多相关文章

  1. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...

  2. POJ 1159 Palindrome(最长公共子序列)

    http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...

  3. POJ - 1159 Palindrome(dp-回文变形)

    d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...

  4. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

  5. poj 1080 基因组(LCS)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19376   Accepted:  ...

  6. HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)

    题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...

  7. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  8. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  9. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

随机推荐

  1. Selenium WebDriver + Grid2 + RSpec之旅(五)---面向对象设计用例

    Selenium WebDriver + Grid2 + RSpec之旅(五) ----面向对象设计用例 前几节讲了怎么一步一步的从零开始到编写出一个简单的测试用例,这一节将要讲一下怎么让测试用例变得 ...

  2. java基础(二十)IO流(三)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  3. 移动平台WEB前端开发技巧

    1.首先我们来看看webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用 <meta content="width=device-width ...

  4. jQuery 参考手册 - 文档操作

    上传图片时页面崩溃..全部付之东流 addClass() after() append() appendTo() attr() before() clone() detach() empty() ha ...

  5. General: Know How to Use InetAddress

    Modern applications often need the ability to learn information about hosts out on the network. One ...

  6. Asp.Net Design Pattern Studynotes -- Part1

    Asp.Net Design Pattern Studynotes -- Part1 let's start with an exampleto entry amazing OO world ! le ...

  7. C# checked关键字当属性

    前端extjs 以及 bootstrap-treeview 插件要用到这个属性,加@符号解决

  8. String与常量池

    转自:http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html 1. 首先String不属于8种基本数据类型,String是一个对象.因为对象的默认值是 ...

  9. 自定义toast功能

    http://download.csdn.net/detail/caryt/8105031

  10. careercup-数组和字符串1.4

    1.4 编写一个方法,将字符串中的空格全部替换为“%20“.假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的”真实“长度. C++实现代码: #include<iostream> ...