<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的更多相关文章

随机推荐

  1. C# 处理oralce 时间

     addWorkSql.Append("to_date(' " + DateTime.Now.ToString("yyyy-MM-dd HH:ss:mm") + ...

  2. JavaScript是按值传递还是按引用传递?

    JavaScript是按值传递的,但是要分情况才知道传递之后原来的值会不会变,不然会出现你想都想不出来的bug 一.按值传递--元类型输入tip:元类型( number, string, boolea ...

  3. Cordova 开发环境搭建及创建第一个app

    整理记录使用cordova创建app应用程序并将其部署至Android系统移动设备上操作过程,具体如下: 一.前期安装环境 1. 安装JDK(java开发工具包) 2. 安装gradle 3. 安装A ...

  4. 跳出双重for循环的案例__________跳出了,则不再执行标签ok下的for循环代码

    ok: for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { System.out.print("*" ...

  5. Android截图截取弹框AlertDialog

    1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" ...

  6. vue.js的ajax和jsonp请求

    首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...

  7. spring实现helloWord

    第一步:添加架包 第二步:写一个简单的实列 package com.java.test; /** * @author nidegui * @create 2019-06-22 10:58 */ pub ...

  8. Firebug全了解

    Firebug是firefox下的一个扩展,能够调试所有网站语言,如Html,Css等,但FireBug最吸引人的就是javascript调试功能,使用起来非常方便,而且在各种浏览器下都能使用(IE, ...

  9. (C/C++学习)1.C++中vector的使用

    说明:vector是C++中一个非常方便的容器类,它用于存放类型相同的元素,利用成员函数及相关函数可以方便的对元素进行增加或删除,排序或逆序等等,下面将对这些功能一一叙述. 一.vector的第一种用 ...

  10. 洛谷P1478 陶陶摘苹果(升级版)【水题】

    又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他手够不着时,他会站到椅子上再试试. 这次与NOIp2005普及组第一题不同的是:陶陶之前搬凳子,力气只剩下s ...