链接:



Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2430    Accepted Submission(s): 838

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
 
Source
 
Recommend
linle




题意:


先给你字符串的长度 N 
再给你 N 个字符组成的序列,问最小多少个操作【添加一些字符】可以将原来的序列变成回文串

分析:

就是逆序存一下当前序列,然后求出 LCS 【最长公共子序列】
用 N 减去所求的最长公共子序列就可以了

注意到序列很长 5000 如果用裸 dp[5000][5000] 就 MLE 了,
以前在 POJ 上做过这道题, 内存是 hdu  的两倍,看过一篇博客是关于记忆化存储的数组开 dp[maxn][maxn] short int 随便就搞过去了,然后前天晚上就一直 MLE 啊Orz 

完了学妹对我说 %2 开个 dp[2][5000] 就可以过了。。。

也就是前一篇博客的滚动数组思想:

hdu 1159 Common Subsequence 【LCS 基础入门】

当前这一行的状态只由上一行的状态和这一行前面的状态确定,所以只有记录两组Orz
也就是说直接把 裸的 LCS 的数组定义改成 dp[2][maxn], 然后把裸的模板中的 i 全部 %2 就好了
最后输出 n - dp[n%2][n]
关于滚动数组 LCS 如果有不了解的自己去上一篇博客看

hdu 1159 Common Subsequence 【LCS 基础入门】


code:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 5005;
int dp[2][maxn];
char s1[maxn], s2[maxn]; void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
scanf("%s", s1);
for(int i = 0; i < n; i++)
s2[i] = s1[n-i-1];
memset(dp,0,sizeof(dp));
LCS(n,n);
printf("%d\n", n-dp[n%2][n]);
}
return 0;
}


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

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

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

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

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

  3. hdoj 1513 Palindrome【LCS+滚动数组】

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

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

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

  5. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

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

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

  7. HDU 1513 Palindrome【LCS】

    题意:给出一个字符串s,问至少加入多少个字母让它变成回文串 解题思路:求出该字符串与该字符串翻转后的最长公共子序列的长度,再用该字符串的长度减去最长公共子序列的长度即为所求 反思:因为题目所给的n的范 ...

  8. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

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

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

随机推荐

  1. 读取properties属性文件——国际化

    public class PropertiesInfo { /** * PropertiesInfo实例 */ private static PropertiesInfo pi = null; pri ...

  2. html5-本地数据库的操作

    <script src="jquery-1.8.3.js"></script><script>/* IE11不支持此操作创建数据库 解释一下op ...

  3. unity, Global和Local编辑模式

    下图表示是在Local模式下: 下图表示是在Global模式下: 不要搞反.

  4. 点滴积累【JS】---JS小功能(button选择颜色)

    效果: 代码: <head runat="server"> <title></title> <style type="text/ ...

  5. [elk]elastalert邮箱告警

    本次要完成以下任务: 1.源码包安装elasticalert 2.配置邮箱报警 原则: 先很快的通过alert报警发一份邮件,其次了解alert配置文件各个选项 源码安装elasticalert 参考 ...

  6. Catalog的种类

    框架中的Catalog 在MEF框架中,包含了4种Catalog,所有的Catalog的是从System.ComponentModel.Composition.Primitives名称空间下的Comp ...

  7. 使用SQLite

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...

  8. matplotlib 添加注释的方式

    matplotlib 添加注释的方式 matplotlib.pyplot.annotate Annotate the point xy with text s s : str The text of ...

  9. 天猫 小游戏 24 point

    游戏规则:给你四个整数,当然他给的是有解的,然后用' +  -  *   /   (  )   ,这几种符号任意组合,使运算结果等于24; 用代码快速解决问题,呵呵... #include<io ...

  10. android定义dialog

    对于一些特殊的dialog,我们想自定义一些特殊的样式.这时候假设使用dialog指定的一些功能.是无法实现我们的需求的,这时候就要自己去定制实现dialog了,如今这个样例是我从stackoverf ...