hdu 1513(滚动数组)
Palindrome
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4751 Accepted Submission(s): 1625
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.
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.
Ab3bd
///题意:将一个字符串变成回文串需要添加的最少字符数量
///解法:将字符串反过来.求出原字符串与现在的字符串的LCS,然后用原串减掉LCS的长度即为最少添加的字符
///这个解法的好处就是可以将DP数组变成滚动数组
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
int dp[][N];
char str[N],str1[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",str+);
for(int i=;i<=n;i++){
str1[n-i+]=str[i];
}
///printf("%s",str1+1);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(str[i]==str1[j]){
dp[i%][j] = dp[(i-)%][j-]+;
}else{
dp[i%][j] = max(dp[i%][j-],dp[(i-)%][j]);
}
}
}
printf("%d\n",n-max(dp[][n],dp[][n]));
}
}
贴个爆内存的代码。。开short都没用,hdu数据强吗 ORZ ~~~
///题意:将一个字符串变成回文串需要添加的最少字符数量
///解法:区间DP,dp[i][j]代表区间i-j里面要添加多少个字符才能将其变成回文串
///如果 str[i] == str[j] 那么 dp[i][j] = dp[i+1][j-1]
///否则 dp[i][j] = min(dp[i+1][j] ,dp[i][j-1])+1 即考虑是在第i+1个字符之前添加str[j]还是在
///在j-1之后添加字符str[i]
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
short dp[N][N];
char str[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",str);
for(int i=;i<=n;i++) dp[i][i]=;///区间长度为1的时候不需要添加就是回文串
for(int l = ;l<=n;l++){
for(int i=;i<=n-l+;i++){
int j = i+l-;
if(str[i]==str[j]) {
dp[i][j] = dp[i+][j-];
}else{
dp[i][j] = min(dp[i+][j],dp[i][j-])+;
}
}
}
printf("%d\n",dp[][n-]);
}
}
hdu 1513(滚动数组)的更多相关文章
- hdu 1024(滚动数组+动态规划)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU - 3033 滚动数组有坑
每层至少一个,滚动时要判上一层非法与否,所以每次都要memset #include<bits/stdc++.h> #define rep(i,j,k) for(int i=j;i<= ...
- hdu 1513(dp+滚动数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 思路:n这么大,可以采用滚动数组,然后就是求原串和反串的LCS了. #include<io ...
- hdu 1513 Palindrome【LCS滚动数组】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...
- hdu 1513 添最少字回文 (Lcs 滚动数组)
http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- hdu 4576 (简单dp+滚动数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 题意:给出1~n的环,m个操作,每次能顺时针或逆时针走w步,询问最后在l~r这段区间内概率.(1 ...
随机推荐
- Coursera: Internet History, Technology, and Security
课程网址:https://www.coursera.org/learn/internet-history 学习笔记: Week 1: History - Dawn of Early Computing ...
- chromium源码阅读--V8 Embbeding
V8是google提供高性能JavaScript解释器,嵌入在chromium里执行JavaScript代码. V8本身是C++实现的,所有嵌入本身毫无压力,一起编译即可,不过作为一个动态语言解释器, ...
- arm单板上移植gdb
虚拟机 : vmware 12 image: ubuntukylin 14.04.1 系统版本:Linux dancy 3.13.0-32-generic #57-Ubuntu SMP Tue Jul ...
- C - 红与黑
C - 红与黑 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descri ...
- [Elasticsearch] 多字段搜索 (一) - 多个及单个查询字符串
多字段搜索(Multifield Search) 本文翻译自官方指南的Multifield Search一章. 查询很少是只拥有一个match查询子句的查询.我们经常需要对一个或者多个字段使用相同或者 ...
- 从pthread 到QThread
该文出自:http://www.civilnet.cn/bbs/topicno/78430 使用线程技术进行应用编程的意义在gemfield的文章<从进程到线程>中已经介绍过了,我们就直奔 ...
- php在类里如何调用call_user_func_array《细说php2》
- P3509 [POI2010]ZAB-Frog
题目描述 On the bed of one particularly long and straight Byteotian brook there lie rocks jutting above ...
- HDU.2095(异或运算)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- http get post 参数校验
spring boot 常见http get ,post请求参数处理 在定义一个Rest接口时通常会利用GET.POST.PUT.DELETE来实现数据的增删改查:这几种方式有的需要传递参数,后台 ...