Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3918    Accepted Submission(s):
1340

Problem Description
A palindrome is a symmetrical string, that is, a string
read identically from left to right as well as from right to left. You are to
write a program which, given a string, determines the minimal number of
characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "Ab3bd" can be
transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer
than 2 characters does not produce a palindrome.

 
Input
Your program is to read from standard input. The first
line contains one integer: the length of the input string N, 3 <= N <=
5000. The second line contains one string with length N. The string is formed
from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and
digits from '0' to '9'. Uppercase and lowercase letters are to be considered
distinct.
 
Output
Your program is to write to standard output. The first
line contains one integer, which is the desired minimal number.
 
Sample Input
5
Ab3bd
 
Sample Output
2
题意:最少向字符串中添加几个字符可以使该字符串变成回文字符串
题解:将字符串反转,跟原字符串使用LCS,算出最长公共子序列长度,拿总长度减去最长公共子序列长度就是答案
#include<stdio.h>
#include<string.h>
#define MAX 5010
#define max(x,y)(x>y?x:y)
char s1[MAX],s2[MAX];
int dp[2][MAX];
int main()
{
int i,j;
int t;
while(scanf("%d",&t)!=EOF)
{
scanf("%s",s1);
for(i=t-1,j=0;i>=0;i--)
s2[j++]=s1[i];
memset(dp,0,sizeof(dp));
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
dp[0][j]=dp[1][j];
dp[1][j]=dp[2][j];
if(s1[i-1]==s2[j-1])
dp[1][j]=dp[0][j-1]+1;
else
dp[1][j]=max(dp[1][j-1],dp[0][j]);
}
}
printf("%d\n",t-dp[1][t]);
}
return 0;
}

  

hdoj 1513 Palindrome【LCS+滚动数组】的更多相关文章

  1. hdu 1513 Palindrome【LCS滚动数组】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

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

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

  4. LCS(滚动数组) POJ 1159 Palindrome

    题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度 ...

  5. hdu 1513 添最少字回文 (Lcs 滚动数组)

    http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表 ...

  6. POJ 1159 回文LCS滚动数组优化

    详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000  的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + ...

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

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

  8. hdu_1513_Palindrome(LCS+滚动数组)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...

  9. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

随机推荐

  1. HTTP could not register URL http://+:8000/.... Your process does not have access rights to this namespace

    windows 7, Visual Studio 2013 在托管 Windows 服务中承载 WCF 服务时报错: HTTP could not register URL http://+:8000 ...

  2. const和volatile的区别

    一.关键字const有什么含意? 只要一听到说:“const意味着常数”,就知道我正在和一个业余者打交道.去年Dan Saks已经在他的文章里完全概括了const的所有用法,因此ESP(译者:Embe ...

  3. 百度地图API地址转换成经纬度

    public class LngAndLatUtil { public static Map<String,Double> getLngAndLat(String address){ Ma ...

  4. SGU 176.Flow construction (有上下界的最大流)

    时间限制:0.5s 空间限制:4M 题意: 有一个由管道组成的网络,有n个节点(n不大于100),1号节点可以制造原料,最后汇集到n号节点.原料通过管道运输.其中有一些节点有管道连接,这些管道都有着最 ...

  5. Codeforces 543C Remembering Strings(DP)

    题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...

  6. phpcms源码跟踪(1)

    本次跟踪解决几个问题: 1.缓存文件从哪里来,过程中被删除了怎么办 2.模板html是如何被引入的 进入首页时,通过最初的调用,进入控制器\phpcms\modules\content\index.p ...

  7. 关于Function()函数对象的那些小九九

    概念:首先,函数是一种特殊类型的数据,函数也是数据类型的一种,实际上函数也是一种对象,函数对象的内建构造器是Function(); 函数的几种创建方式: 函数声明法: function sum(a,b ...

  8. App适配iPhone 6/ Plus和iOS 8:10条小秘诀

    App适配iPhone 6/ Plus和iOS 8:10条小秘诀   iPhone 6iOS 8适配   (原文:raywenderlich 作者:Jack Wu 译者:@TurtleFromMars ...

  9. phalapi

    public为程序入口 Demo和MyApp为具体的实现 public为多入口 demo和myapp都是入口,但是进入后即又为单入口 list为接口文档,是自动解析程序里的注释自动生成的文档 框架执行 ...

  10. Yosemite重置Dock的命令

    备忘 defaults write com.apple.dock ResetLaunchPad -bool true killall Dock