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的更多相关文章
随机推荐
- 浅谈自学Python之路(day3)
今天的主要内容是: 撒 文件操作 对文件操作的流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: tonghuazhen 听说白雪公主在逃跑 小红帽在担心 ...
- unity3D 使用欧拉角控制视野注意点
变量声明: public PlayerInput p; //表示控制代码用来获得用户是否按下 public float rotateSpeed = 50f; //旋转速度 private GameOb ...
- JavaScript学习二
2019-05-30 15:08:24 加油,这几天在赶高数,都…… <!DOCTYPE html> <html> <head> <script type=& ...
- Js:弹窗剧中
js变量设置 var iWidth = $(window).width() * 0.9; var iHeight = $(window).height() * 0.9; - iHeight) / ; ...
- Cracking the Coding Interview 8.7
Given a infinite number of quarters(25cents), dimens(10cents), nickels(5cents) and pennies(1cent), w ...
- 错误:android.view.InflateException: Binary XML file line #167: Binary XML file line #167: Error inflating class <unknown>
1:错误日志 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.8.activity.RecordActiv ...
- easyui combobox的增加全选解决方案
1.解决方案背景: 项目中偶然需要用到easyui的combobox的组件,但是本组件自己没有包含全选的api事件.搜索了一些解决方案,但是不是很符合,后来发现是因为所使用的版本不一致所导致的.项 ...
- VTK初始化New返回Null问题
原文链接:http://www.cppblog.com/mythma/archive/2013/08/02/vtk-6-new-null.html 在使用VTK6.0时候,会遇到X::New()返回为 ...
- 不能访问windows installer 服务,可能你在安全模式下运行 windows ,或者windows installer
windows installer服务解决方案 很多朋友在安装MSI格式的文件包时,经常会遇到windows installer出错的情况,有如下几种现象: 1.所有使用windows install ...
- day001 Python 计算机基础(2019年5月16日)
      作为一名程序员或者即将踏入IT行业的准程序员,学习任何一门编程语言,都需要有基本的计算机基础 ...