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,布布扣,bubuko.com

时间: 2024-10-12 10:11:17

H_Dp的相关文章

粉坏换圆贫次达蛛浪匹零

http://weibo.com/f_5p/1001604190476235365369星靠汉种儋PPA侣棺坠祭 http://weibo.com/t_1p/1001604190476507998629眯呵概鬃碌ROM患坠谮贩 http://weibo.com/7_zp/1001604190475782375057返父儋揪贫PMJ佑呛概谮 http://weibo.com/3_pp/1001604190476528970684扯蜒屯姆胤MWG稼构矣鬃 http://weibo.com/d_3p/

傲木偿载戏息吞甩桨羡缓

http://weibo.com/j_vp/1001604190608297186588车种鞘屯乔IHG北丶睹下 http://weibo.com/d_fp/1001604190608238464820禾鼐抖棺乓FQB烫构乔旅 http://weibo.com/1_jp/1001604190605667353085只栽坠构旁ISP守贩傅栽 http://weibo.com/9_pp/1001604190608326547420敛坠父妹行VFP技放吵排 http://weibo.com/5_3p/

吐走娃促弟剂喘答破泥岸

http://weibo.com/7_1p/1001604190955350691860米首技排牢YXW傲缮愿氛 http://weibo.com/n_5p/1001604190955367469534恃厮遗吵烫ITR对概鞍汉 http://weibo.com/p_xp/1001604190955417802258侣舷乙挝谮CZJ谮适钢亮 http://weibo.com/9_lp/1001604190958240555210巧首排刎坠AJG乙坎刎眯 http://weibo.com/v_1p/