zoukankan      html  css  js  c++  java
  • String painter (区间DP)

    String painter

     HDU - 2476 

    There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?

    InputInput contains multiple cases. Each case consists of two lines: 
    The first line contains string A. 
    The second line contains string B. 
    The length of both strings will not be greater than 100. 
    OutputA single line contains one integer representing the answer.Sample Input

    zzzzzfzzzzz
    abcdefedcba
    abababababab
    cdcdcdcdcdcd

    Sample Output

    6
    7
    题意:给你A,B两个字符串,长度相等,问你最小的操作步数可以使得A串变成B串,每次操作选择一个区间将该区间全变成同一个字母。
    题解:
      先假设A串是空串,计算一下A变成B需要的最小操作数,然后定义一个ans数组 ans{i]表示从A串从0到i想要变成B串需要的最小操作步数,如果A[i] == B[i],那么ans[i]=ans[i-1]否则ans[i]最大为从0到i A串全部字母都与B串不相等的最小操作步数,因为也有可能0-i中有A和B相同的字母 所以遍历一下,更新最小值。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 char s1[110],s2[110];
     7 int dp[110][110];
     8 int ans[110];
     9 int main()
    10 {
    11     while(~scanf("%s",s1))
    12     {
    13         memset(dp,0,sizeof(dp));
    14         memset(ans,0,sizeof(ans));
    15         scanf("%s",s2);
    16         int n=strlen(s1);
    17         for(int i=0;i<=n;i++)
    18             dp[i][i]=1;
    19         for(int len=1;len<=n;len++)
    20         {
    21             for(int l=0;l+len<n;l++)
    22             {
    23                 int r=l+len;
    24                 if(s2[l]==s2[r])
    25                     dp[l][r]=dp[l][r-1];
    26                 else
    27                 {
    28                     dp[l][r]=dp[l][r-1]+1;
    29                     for(int k=l;k<=r;k++)
    30                     {
    31                         dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);
    32                     } 
    33                 }
    34                     
    35             }
    36         }
    37         if(s1[0]!=s2[0])
    38             ans[0]=1;
    39         for(int i=1;i<n;i++)
    40         {
    41             if(s1[i]==s2[i])
    42                 ans[i]=ans[i-1];
    43             else
    44             {
    45                 ans[i]=dp[0][i];
    46                 for(int j=0;j<i;j++)
    47                 {
    48                     ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
    49                 }
    50             }
    51         }
    52         printf("%d
    ",ans[n-1]); 
    53     }
    54 }
  • 相关阅读:
    看到一个很好看的细细的字体
    meta标签常用属性整理
    网页字体排印指南
    利用伪元素仿苹果手机官网导航
    利用css3-animation来制作逐帧动画
    CSS实现垂直居中的5种方法
    前端网站
    icon font
    flexbox布局总结
    Yahoo(YUI) CSS Reset:
  • 原文地址:https://www.cnblogs.com/1013star/p/10616261.html
Copyright © 2011-2022 走看看