<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. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

  2. 开启远程MySQL

    安装完MySQL,由于安全原因默认是没有赋予用户远程权限的,所以第一步要首先赋予用户对应的权限 一  授权 mysql> mysql -u用户名 [-pIp地址] -p #登录 mysql> ...

  3. bootstrap-paginator基于bootstrap的分页插件

    bootstrap-paginator基于bootstrap的分页插件 GitHub 官网地址:https://github.com/lyonlai/bootstrap-paginator 步骤 引包 ...

  4. React Component(dva)

    Stateless Functional Components(3种方式) class App extends React.Component function App() const App= Re ...

  5. PDO获取数据乱码的解决方法

    确保PHP文件编码格式为UTF8 确保数据字段格式为UTF8 PDO中设置编码格式,有如下三种方式: 方式1: 写在初始化dsn中 define( 'DB_DSN', 'mysql:host=loca ...

  6. ListView使用、ListView优化和遇到的问题

    1.先写遇到的问题: a.ListView只显示一个item. listview只显示一个item,并且做了listview的点击事件监听打印 Bean 对象的属性和哈希值,发现只有显示的那个 Bea ...

  7. ie8及其以下版本兼容性问题之响应式

    解决办法:引入Respond.js让IE6-8支持CSS3 Media Query 使用方式 参考官方demo:http://scottjehl.github.com/Respond/test/tes ...

  8. centos开机运行级别更改

    1.使用命令切换运行级别/目标 # systemctl isolate multi-user.target //切换到运行级别3,该命令对下次启动无影响,等价于telinit 3 # systemct ...

  9. OpenCV:Adaboost训练时数据扩增

    更准确的模型需要更多的数据,对于传统非神经网络机器学习方法,不同的特征需要有各自相符合的数据扩增方法. 1.   在使用opencv_traincascade.exe 过程中,图像读取在 classi ...

  10. C# 获取 IEnumerable 集合的个数

    IEnumerable<DocApply> data1 = data.Where(n => n.DocName.Contains(search)); if (data1.GetEnu ...