zoukankan      html  css  js  c++  java
  • hdu 2476 String Painter

    第一道区间dp题,感觉题意不是很好理解

    题意:一次可以转换某一个位置的字符,或是一串连续的字符,举第一个例子zzzzzfzzzzz

    1:aaaaaaaaaaa

    2: abbbbbbbbba

    3: abcccccccba

    4: abcdddddcba

    5: abcdeeedcba

    6: abcdefedcba

    于是第一个例子输出6,第二个同理

    话不多说,直接贴一波代码

     1 #include<cstdio>  
     2 #include<iostream>  
     3 #include<algorithm>
     4 #include<math.h> 
     5 #include<string.h>  
     6 #include<vector> 
     7 #include<queue>
     8 #include<map>
     9 #include<iterator>
    10 #include<vector>
    11 #include<set>
    12 #define INF 9999999
    13 typedef long long ll;
    14 const int Max=(1<<16)+10;
    15 using namespace std;
    16 
    17 int dp[1005][1005];//dp[i][j]表示从i~j的刷法 
    18 int ans[1005]; 
    19 
    20 int main()
    21 {
    22     string str1,str2;
    23     int len;
    24     while(cin>>str1>>str2)
    25     {
    26         len=str1.length();
    27         for(int j=0;j<len;j++)
    28         {
    29             for(int i=j;i>=0;i--) 
    30             {
    31                 dp[i][j]=dp[i+1][j]+1;//逐个更新区间i~j内的值 
    32                 for(int k=i+1;k<=j;k++)
    33                 {
    34                     if(str2[i]==str2[k])//若遇到相同的则需寻找区间的最小值 
    35                         dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
    36                 } 
    37             }
    38         }
    39         
    40         for(int i=0;i<len;i++)
    41             ans[i]=dp[0][i];//ans[i]表示区间[0,i]需要刷的值 
    42         for(int i=0;i<len;i++)
    43         {
    44             if(str1[i]==str2[i])//碰到相同的值选择不刷 
    45                 ans[i]=ans[i-1];
    46             else
    47             {
    48                 for(int j=0;j<i;j++)//寻找最优解 
    49                     ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
    50             }
    51         }    
    52         cout<<ans[len-1]<<endl;
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    jmeter在Windows下安装(含插件安装)
    Jenkins中agent的使用
    Jenkins自动化测试脚本的构建
    Python在Linux下编译安装
    Jenkins项目构建运行
    VIM不正常退出产生的swp文件
    SSI服务器端包含注入
    【强网杯2019】随便注
    判断网站CMS
    windows基础
  • 原文地址:https://www.cnblogs.com/pter/p/5506787.html
Copyright © 2011-2022 走看看