H_Dp
<span style="color:#000099;">/*
H - 简单dp 例题扩展
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
By Grant Yuan
2014.7.16
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char a[5002];
char b[5003];
int dp[2][5003];
int n;
int max(int aa,int bb){
return aa>=bb?aa:bb;
} int main()
{
while(~scanf("%d",&n)){
scanf(" %s",&a);
for(int i=0;i<n;i++)
b[n-1-i]=a[i]; // puts(a);
// puts(b);
memset(dp,0,sizeof(dp));
int flag=0,flag1=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(a[i]==b[j])/*{flag1=0;
if(flag==0)
dp[1][j+1]=dp[0][j]+1,flag=1;
else
dp[0][j+1]=dp[1][j]+1,flag=0;
}*/
dp[(i+1)&1][j+1]=dp[i&1][j]+1;
else
{/*flag1=1;
if(flag==0)
dp[1][j+1]=max(dp[0][j+1],dp[1][j]),flag=1;
else
dp[0][j+1]=max(dp[1][j+1],dp[0][j]),flag=0;*/
dp[(i+1)&1][j+1]=max(dp[(i+1)&1][j],dp[i&1][j+1]);
}
//if(flag1==0)cout<<"相等";
//else cout<<"不相等";
// cout<<"flag: "<<"i: "<<flag<<" "<<i<<" "<<j<<" "<<dp[flag][j+1]<<endl;
// system("pause");
}
int l;
/*if(flag==1)
l=dp[1][n];
else
l=dp[0][n];*/
l=dp[n&1][n];
cout<<n-l<<endl;}
return 0;
}
</span>
H_Dp的更多相关文章
随机推荐
- html5 窗口之间的通信
一般窗口通信分为三种: iframe嵌套:多个iframe之间通信. 父页面操作子页面元素:oFrame.contentWindow.document.body. 父页面调用子页面方法:oFrame. ...
- C - cAPS lOCK
Problem description wHAT DO WE NEED cAPS LOCK FOR? Caps lock is a computer keyboard key. Pressing it ...
- myeclipse2014 破解步骤
1.打开破解文件夹Myeclipse 2014 patch,运行run.bat文件 2.在破解界面中,usercode随便输入, systemid 在右边的SystemId按钮处,点击. 将自动生成一 ...
- 取消VS2017窗口置顶
今天打开VS2017,莫名其妙窗口置顶了,百度了一下如何取消窗口置顶,就是Ctrl+Alt+Esc组合键,就可以取消窗口置顶了,至于到底怎么会突然置顶的我也不知道emmm... /********** ...
- intellij IDEA常见操作
1.中文乱码设置:file - setting - Editor - File Encodings 设置为UTF-8 2.tomcat重新启动:Ctrl-F5,或者左上角 3.删除progect 先c ...
- Error:CreateProcess error=216的错误,JDK版本不匹配问题。
今天刚下载安装完android studio,结果随便新建一个工程的时候就出现了如下提示: Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版 ...
- SSIS SQL Server配置自动作业
目录: 一. 用SSMS配置作业,自助调度: 二.用SSMS调SSIS包: 一. 用SSMS配置作业,自助调度: 为验证数据,先创建一个表: CREATE TABLE test_table (id I ...
- 《Linux程序设计》笔记(二)shell程序设计
1. 进程树形显示 ps -e f 2. 重定向 > 覆盖文件 >> 附加至文件 1> 标准输出 2> 标准错误输出 0 代表一个程序的标准输入 3. 程序可以在当前目录 ...
- CNN结构:用于检测的CNN结构进化-一站式方法
有兴趣查看原文:YOLO详解 人眼能够快速的检测和识别视野内的物体,基于Maar的视觉理论,视觉先识别出局部显著性的区块比如边缘和角点,然后综合这些信息完成整体描述,人眼逆向工程最相像的是DPM模型. ...
- MxNet教程:使用一台机器训练1400万张图片
官网链接:http://mxnet.readthedocs.io/en/latest/tutorials/imagenet_full.html Training Deep Net on 14 Mill ...