hdoj 1513 Palindrome【LCS+滚动数组】
Palindrome
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3918 Accepted Submission(s):
1340
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.
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.
line contains one integer, which is the desired minimal number.
#include<stdio.h>
#include<string.h>
#define MAX 5010
#define max(x,y)(x>y?x:y)
char s1[MAX],s2[MAX];
int dp[2][MAX];
int main()
{
int i,j;
int t;
while(scanf("%d",&t)!=EOF)
{
scanf("%s",s1);
for(i=t-1,j=0;i>=0;i--)
s2[j++]=s1[i];
memset(dp,0,sizeof(dp));
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
dp[0][j]=dp[1][j];
dp[1][j]=dp[2][j];
if(s1[i-1]==s2[j-1])
dp[1][j]=dp[0][j-1]+1;
else
dp[1][j]=max(dp[1][j-1],dp[0][j]);
}
}
printf("%d\n",t-dp[1][t]);
}
return 0;
}
hdoj 1513 Palindrome【LCS+滚动数组】的更多相关文章
- 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+滚动数组)
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求 ...
- LCS(滚动数组) POJ 1159 Palindrome
题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度 ...
- hdu 1513 添最少字回文 (Lcs 滚动数组)
http://blog.csdn.net/ice_crazy/article/details/8244639 这里5000*5000超出内存,所以需要用滚动数组: 用一个now表示当前的结果,pre表 ...
- POJ 1159 回文LCS滚动数组优化
详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000 的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + ...
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- hdu_1513_Palindrome(LCS+滚动数组)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
随机推荐
- treeview右键添加新节点
private void advTree1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Righ ...
- Swift 中 Selector 方法的访问权限控制问题
今天用Swift写了个视图,在视图上加个手势,如下所示: panGestureRecognizer = UIPanGestureRecognizer(target: self, action: &qu ...
- PHP算法 《图》 之 理论基础
转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...
- 292. Nim Game(C++)
292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...
- C# WinFrom 编写正则表达式验证类
public class ValidationRegex { /// <summary> /// 正则表达式字符串 /// </summary> public static s ...
- php 钩子函数原理 解析
目前对钩子的理解:<转载:http://www.cnblogs.com/del/archive/2008/02/25/1080825.html> 譬如我们用鼠标在某个窗口上双击了一次, 或 ...
- TCP协议握手与分手
TCP(Transmission Control Protocol) 传输控制协议 TCP的7次握手可以理解为3次握手和4次分手. TCP状态转换图,如下: 这个图N多人都知道,它对排除和定位网络或系 ...
- BTREE与其它索引的优缺点对比
数据库BTree索引.Hash索引.Bitmap位图索引的优缺点 (2016-01-05 17:13:40) 转载▼ 标签: 数据库 索引 mysql oracle 分类: IT http://www ...
- arm-none-eabi-gcc install
Zephyr除了官方的编译工具,还有第三方工具 arm-none-eabi-gcc . This PPA is an alternative to toolchain released at http ...
- c# appdomain
http://www.cnblogs.com/Terrylee/archive/2005/11/28/285809.html