Palindrome

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

Total Submission(s): 3919    Accepted Submission(s): 1341

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
 
好难好难得一道题。刚開始没有思路。后来想到把数组反转存起来用lcs,代码都写得收尾了发现,dp数组定到5000*5000妥妥的超内存。后来听大神说还要用滚动数组,就试了试。效果蛮好的,非常开心的一次a题,一下就a了,下次还问大神,五星好评哦。

#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
char c[5050],s[5050];
int dp[2][5050];//滚动数组
int i,n,m,j;
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
scanf("%s",c);
for(i=0;i<n;i++)
s[i]=c[n-i-1];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
int x=i%2;//由于模拟一下,第一维仅仅用了两个数。所以循环使用即可了,仅仅要第二维改变着。总体就改变着
int y=(i-1)%2;//lcs模板
if(s[j-1]==c[i-1])
dp[x][j]=dp[y][j-1]+1;
else
dp[x][j]=max(dp[y][j],dp[x][j-1]);
}
int sum=dp[n%2][n];
printf("%d\n",n-sum);
}
return 0;
}

杭电1513Palindrome的更多相关文章

  1. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  2. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  3. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  6. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  7. 杭电ACM2076--夹角有多大(题目已修改,注意读题)

    杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...

  8. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

  9. 杭电2034——人见人爱A-B

    #include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...

随机推荐

  1. css媒体查询:响应式网站

    css媒体查询:响应式网站 媒体查询 包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3加入的媒体查询使得无需修改内容便可以使样式应用于某些特定的设备范围. ...

  2. Linux 文件系统模型

    声明:本文仅限于 cnblogs 发布,其他第三方网站均为盗版,原文地址:Linux 文件系统模型 在 Linux 环境下有过一些经历的同学可能都会遇到一个问题,这个问题就是往机器上插入 U盘 或者其 ...

  3. 收集—— css实现垂直居中

    Method1: 在父元素上设置display:table-cell;vertical-align:middle(父元素不能设置浮动) Method2: 使用flex:父元素设置成display: f ...

  4. RabbitMQ消息队列系列教程(一)认识RabbitMQ

    摘要 RabbitMQ是最为流行的消息中间件,是处理高并发业务的利器.本系列教程,将跟大家一起学习RabbitMQ. 目录 RabbitMQ是什么? RabbitMQ的特点是什么? 一.RabbitM ...

  5. http2.4简单配置

    前言: 上一篇博文说到了http的发展以及http完整请求响应的工作流程. 一.开篇: 从最简单的静态服务器开始. 之前说过,http是应用层协议,必定会在用户空间体现出具体的应用程序.常见的http ...

  6. CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)

    一 安装篇 1. 安装nginx yum check-update #更新yum源 yum remove httpd* php* #删除系统自带的软件包 yum install nginx #安装ng ...

  7. JavaScript的兼容

    兼容总结 如果两个都是属性,用逻辑 || 做兼容 如果有一个是方法,用三元做兼容 如果多个属性或方法,封装函数做兼容 获取class属性值的兼容 function getClass (obj){ if ...

  8. 关于如何获取移动端 touchmove 事件中真正触摸点下方的元素

    移动端的touchstart, touchmove, touchend三个事件,点击元素并拖动时,获取到了touchmove事件, 但是event.touches[0].target所指向的元素却是t ...

  9. JavaScript学习笔记(十一)——闭包

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  10. 【正则表达式】--python(表示字符)

    [前修知识] match :匹配    span:范围 match 是从头往后开始匹配,search不按照顺序,直接获取自己想要的,有就显示,没有就None r 代表反转义,前面也提到过这个知识,如果 ...