<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. Python 33(2)进程理论

    一:什么是进程         进程指的是一个正在进行 / 运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系统,进程是操作 ...

  2. netty之ByteBuf详解

    [ChannelPromise作用:可以设置success或failure 是为了通知ChannelFutureListener]Netty的数据处理API通过两个组件暴露——abstract cla ...

  3. SfMLearner论文笔记——Unsupervised Learning of Depth and Ego-Motion from Video

    1. Abstract 提出了一种无监督单目深度估计和相机运动估计的框架 利用视觉合成作为监督信息,使用端到端的方式学习 网络分为两部分(严格意义上是三个) 单目深度估计 多视图姿态估计 解释性网络( ...

  4. A - High School: Become Human

    Problem description Year 2118. Androids are in mass production for decades now, and they do all the ...

  5. RabbitMQ 官方NET教程(五)【Topic】

    在上一个教程中,我们改进了我们的日志记录系统.我们使用direct类型转发器,使得接收者有能力进行选择性的接收日志,,而非fanout那样,只能够无脑的转发 虽然使用direct类型改进了我们的系统, ...

  6. AES && DES加解密

    MD5加密一般不可逆,只能暴力突破.所以这边记录一下一些关于字符串的加解密的两种方法,以便自己学习 AES public class AESHelper { public static string ...

  7. Dubbo的@Reference和@Service说明---1Reference用在消费者2Service用在提供者【import com.alibaba.dubbo.config.annotation.Service;】

    @Reference 用在消费端,表明使用的是服务端的什么服务@RestControllerpublic class RemoteUserController { @Reference(version ...

  8. SQLServer2008 将“单个用户”改为“多用户”

    一开始是要想要分离掉数据库,然后将其删除 不知道为什么一直分离不了,试了很多次,又尝试直接删除 结果数据库突然显示成了“单个用户” 尝试查看其属性,或者“新建查询”也都报错,提示已经有其他用户建立了连 ...

  9. js 全选选框与取消全选代码

    设置一个全选选框和四个子选框,要实现点击全选后四个子选框选中,取消全选后四个子选框也取消.全选后点击某个子选框,全选也能取消.当四个子选框都选中时,全选框也被选择. 实现代码: <script& ...

  10. Python之first script

    1 A first script 1) script1.py - imports a Python module (libraries of additional tools) to fetch th ...