hdu 1513 添最少字回文 (Lcs 滚动数组)
http://blog.csdn.net/ice_crazy/article/details/8244639
这里5000*5000超出内存,所以需要用滚动数组:
用一个now表示当前的结果,pre表示前一个的结果,不断滚动即可
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 5005
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; char a[MAXN],b[MAXN]; int dp[][MAXN]; int lcs(int al)
{
int i,j;
int now,pre;
for(i=;i<=al;i++)
{
for(j=;j<=al;j++)
{
now = i%;
pre = -now;
if(a[i-] == b[j-]) dp[now][j] = dp[pre][j-]+;
else dp[now][j] = max(dp[pre][j],dp[now][j-]);
}
}
return al-dp[al%][al];
}
int main()
{
int i,j;
while(~sf("%d",&n))
{
mem(dp,);
sf("%s",a);
strcpy(b,a);
strrev(b);
pf("%d\n",lcs(n));
}
}
hdu 1513 添最少字回文 (Lcs 滚动数组)的更多相关文章
- POJ 1159 回文LCS滚动数组优化
详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000 的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- URAL 1297 最长回文子串(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- hdu 1513 Palindrome【LCS滚动数组】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- HDU 3068:最长回文(Manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的 ...
- Girls' research - HDU 3294 (Manacher处理回文串)
题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...
- HDU - 5785:Interesting (回文树,求相邻双回文的乘积)
Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three ...
随机推荐
- Python——零基础向-四行代码下载网页上的一张图片
一.确保安装了requests模块 还没安装的可以百度一下如何安装,很简单的. 这里简单的说一下方法:win+R,输入cmd,打开命令行窗口,输入命令:pip install requests ,即可 ...
- Python3用sys和time模块实现进度条
import sys import time def view_bar(num, total): rate = float(num) / float(total) rate_num = int(rat ...
- mysql自动备份并上传至git仓库
自动备份 备份需求 数据库备份的重要性再怎么强调也不为过.当你的操作出现差错,但又因为没有作备份导致数据无法还原时,你就能体会到“万念俱灰”的心情了. 数据库备份有多种形式,本文以我的个人网站数据 ...
- StyleSheet
StyleSheet.create()方法 //定义组件 var App = React.createClass({ render:function () { return( <View sty ...
- tornado 08 数据库-ORM-SQLAlchemy-表关系和简单登录注册
tornado 08 数据库-ORM-SQLAlchemy-表关系和简单登录注册 引言 #在数据库,所谓表关系,只是人为认为的添加上去的表与表之间的关系,只是逻辑上认为的关系,实际上数据库里面的表之间 ...
- liunx 请求服务器连接数相关设置
一. 文件数限制修改 1.用户级别 修改 nr_open 限制 (用途:能够配置nofile最大数) cat /proc/sys/fs/nr_open Linux 内核 2.6.25 以前,在内核里面 ...
- nginx(一)- 初识
前言 这里呢,开始我的nginx学习记录.关于nginx是什么?为什么?有什么好处?我就不说了,说的肯定没有网上其他人说的那么细.我在这里就记录我自己学习nginx的过程.只说怎么做,让我们快速用起来 ...
- C++ STL之Vector
向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. vector 属于STL(Standard Template Library, 标准模板库)中的一种自定义的 ...
- hibernateUtil类
package com.test.Util; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfi ...
- [转] Tomcat 禁用URL中的JSESSIONID
[From] http://stackoverflow.com/questions/962729/is-it-possible-to-disable-jsessionid-in-tomcat-serv ...