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 ...
随机推荐
- 软工实践 - 第二十九次作业 Beta 冲刺(7/7)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10159251.html 作业博客:[班级博客本次作业的链接] (https://edu.cnb ...
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...
- Packet filtering with Linux & NAT
http://www.linuxfocus.org/ChineseGB/May2003/article289.shtml Gateway, Proxy-Arp 和 Ethernet Bridge ? ...
- [剑指Offer] 7.斐波那契数列
class Solution { public: int Fibonacci(int n) { ] = {}; res[] = ; res[] = ; ;i < n;i ++){ res[i] ...
- 【python】python各种类型转换-int,str,char,float,ord,hex,oct等
[python] int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) ...
- 制作Windows10政府版的小白教程
制作Windows10政府版的小白教程 https://03k.org/make10entg.html 首先,宿主系统要比操作的系统新,因为低版本dism操作不了: 当然也可以单独下载ADK,提取最新 ...
- Linux总结(二)
1. 虚拟机安装 a)双系统(不建议初学者一开始去装) b)般建议使用虚拟机来操作试验环境 c)好处:可以模拟真实的环境进行各种的试验和操作 d)在启动之后,在操作的时候会占用一部分的系统资源 1 ...
- React生命周期总结
React的生命周期总共8个钩子,三个will,两个Did,一个RecciveProps,一个ShouldUpdate,一个render.分为三个阶段,分别是 装载 Mounting更新 Updati ...
- bzoj进度条
好久没发进度了 这个月没有上个月那么猛,肯能使因为这个月不想水题吧 No. 510 Solved Problems List Solved 368 10001001100210071008101210 ...
- [SCOI2012]喵星球上的点名——堪称十种方法做的题
题意: 给你N个串对,M个询问串,对每个询问串求是多少串对的子串(在串对的某一个中作为子串),以及每个串对最终是包含了多少询问串 方法众多.. 可谓字符串家族八仙过海各显神通. 复杂度不尽相同,O(n ...