H - 简单dp 例题扩展

Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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
题目大意:给你一个字符串,让你往其中插入字符,使其变成回文字符串,问最少需要插入多少字符。
思路分析:所谓回文字符串,就是正读和倒读一样的,因此可以考虑构造两个字符串,另一个字符串是
原字符串的逆序串,然后求最长公共子序列长度,那么字符串长度减去最长公共字符串长度就是需要插入
的字符数,注意dp数组开short,否则会爆内存。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int maxn=5000+100;
short dp[maxn][maxn];
int main()
{
    int n;
    char s1[maxn],s2[maxn];
    scanf("%d",&n);
       cin>>s1;
       for(int i=n-1;i>=0;i--)
        s2[n-i-1]=s1[i];
       memset(dp,0,sizeof(dp));
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               if(s1[i]==s2[j])
               {
                   if(i>=1&&j>=1)
                    dp[i][j]=dp[i-1][j-1]+1;
                   else dp[i][j]=1;
               }
               if(s1[i]!=s2[j])
               {
                   if(i==0&&j==0) dp[0][0]=0;
                   else if(i>=1&&j==0) dp[i][j]=dp[i-1][j];
                   else if(i==0&&j>=1) dp[i][j]=dp[i][j-1];
                   else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
               }
           }
       }
       cout<<(n-dp[n-1][n-1])<<endl;
    return 0;
}
优化:如果开数组dp[maxn][maxn],非常耗内存,如果不用short开就会MLE.但是我们注意到,
dp[i][j]只与上一层的dp[i-1][j-1]或dp[i-1][j]有关,因此可以采用就地滚动的方法节省
内存,只需要开一个dp[2][maxn]的数组,同时,字符串在储存的时候也有技巧,推荐从1-n储存,
这样dp边界处理起来就容易的多。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int maxn=5000+100;
const int inf=0xfffffff;
short dp[2][maxn];//滚动数组
int main()
{
    int n;
    char s1[maxn],s2[maxn];
    scanf("%d",&n);
       scanf("%s",s1+1);
       for(int i=n;i>=1;i--)
        s2[n-i+1]=s1[i];
       memset(dp,0,sizeof(dp));
       int ma=-inf;
       for(int i=1;i<=n;i++)
       {
           for(int j=1;j<=n;j++)
           {
               if(s1[i]==s2[j])
               {
                    dp[i%2][j]=dp[(i-1)%2][j-1]+1;
               }
               if(s1[i]!=s2[j])
               {
                    dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
               }
               if(dp[i%2][j]>ma) ma=dp[i%2][j];
           }
       }
       cout<<(n-ma)<<endl;
    return 0;
}

poj1159 dp(滚动数组优化)的更多相关文章

  1. HDU_1024.MaxSumPlusPlus(基础DP + 滚动数组优化讲解)

    这道题打破了我常规的做题思路,因为这是我刚开始训练DP,感觉这道题目好晕眼呀,emm其实就是感觉自己是真的菜...... 为什么说打破了我的做题思路呢,因为我平时看题解都是在已经AC或者完全不懂的情况 ...

  2. [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  3. LG3004 「USACO2010DEC」Treasure Chest 区间DP+滚动数组优化

    问题描述 LG3004 题解 把拿走的过程反向,看做添加的过程,于是很显然的区间DP模型. 设\(opt_{i,j}\)代表区间\([i,j]\)中Bessie可以获得的最大值,显然有 \[opt_{ ...

  4. 51Nod 1084 矩阵取数问题 V2 双线程DP 滚动数组优化

    基准时间限制:2 秒 空间限制:131072 KB  一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上.第1遍时只能向下和向右走,第2遍时只能向 ...

  5. 2014年北京 happy matt friends(dp + 滚动数组优化)

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Oth ...

  6. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  7. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

  8. dp,滚动数组优化

    51Nod1084矩阵取数问题 V2 题意: 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上.第1遍时只能向下和向右走,第2遍时只能向上和向左 ...

  9. hdu 3392(滚动数组优化dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others)    Me ...

随机推荐

  1. OSG调试信息显示

    调试信息显示 OSG 可以将各式各样的调试信息输出到std:cout.这在开发OSG 程序时十分有用,你可以借此观察OSG 的执行的各种操作.环境变量OSG_NOTIFY_LEVEL用于控制OSG调试 ...

  2. [置顶] chinayaosir近10年来所阅读的世界著名IT书籍-图文并茂

    1.人生观(包括做人原则,心理学,投资,销售) 一个人从来到世上,很多东西都是空白, 阅读一些正能量的书籍,把里面的理论用于生活,不断的应用它, 这些观念就会如同软件一样,不断的升级你的大脑, 合理的 ...

  3. 安装hadoop2.6.0伪分布式环境

    集群环境搭建请见:http://blog.csdn.net/jediael_lu/article/details/45145767 一.环境准备 1.安装linux.jdk 2.下载hadoop2.6 ...

  4. 移动端-jquery Ajax执行滑动到底部进行加载 指定加载次数

    过完年了,又要有新的项目了,趁暴风雨前的宁静,就把之前让我挺头疼的ajax加载给总结了一下.在此分享. 声明下:笔者对于ajax也是新手,如有更好的写法或可以优化的写法,还望指正! 项目需求: 移动端 ...

  5. web标准(复习)--1

    XHTML CSS基础知识 一.xhtml css基础知识首先说一下我们这节课的知识点 1.文档类型 2.语言编码 3.html标签 4.css样式 5.css优先级 6.css盒模型组成 1)文档类 ...

  6. 在jsp页面中使用自定义标签

    在某些场景中,自定义标签可封装大量代码,使页面变得更简洁,标签也可以很方便地在不同页面中实现通用而不必去粘贴大量的js代码.现在把最近做的一个自定义标签在这里总结一下.首先总结一下关于自定义标签的一些 ...

  7. DatePickerDialog 控制只选择年月或年或月

    etXyLevelDate.setOnTouchListener(selectDateTouchListener()); /** * @desc 选择日期操作 * @param @return * @ ...

  8. LeetCode_Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. Smartcard CA智能卡之调试

    Integrated Circuit Card  集成电路卡,也叫CA卡或智能卡,将一个微电子芯片嵌入符合ISO 7816标准的卡基内,做成卡片形式,也是一个嵌入式小系统.由CPU,ROM,RAM及E ...

  10. 微软的操作系统中让 32 位支持大于 4GB 的内存。

    先给一个参考文献:The RAM reported by the System Properties dialog box and the System Information tool is les ...