HDU-2476 String painter

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

http://acm.hdu.edu.cn/showproblem.php?pid=2476

                  String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1580    Accepted Submission(s): 703


Problem DescriptionThere 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 Inputzzzzzfzzzzz abcdefedcbaabababababab cdcdcdcdcdcd 

 

Sample Output67

題意:

給出兩個串s1和s2,一次只能將一個區間刷一次,問最少幾次能讓s1=s2

例如zzzzzfzzzzz,長度為11,我們就將下標看做0~10

先將0~10刷一次,變成aaaaaaaaaaa

1~9刷一次,abbbbbbbbba

2~8:abcccccccba

3~7:abcdddddcba

4~6:abcdeeedcab

5:abcdefedcab

這樣就6次,變成了s2串了

第二個範例也一樣

0

先將0~10刷一次,變成ccccccccccb

1~9刷一次,cdddddddddcb

2~8:cdcccccccdcb

3~7:cdcdddddcdcb

4~6:cdcdcccdcdcb

5:cdcdcdcdcdcb

最後竟串尾未處理的刷一次

就變成了串2cdcdcdcdcdcd

所以一共7次

先是考慮將所有與目標字串不相同的刷成目標串:

dp[i][j]表示刷i-j區間,

初始條件:dp[i][j]=dp[i+1][j]+1;

 

對於k=(i+1...j )如果str[k]==str[i],則dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]),,因為刷i的時候可以與k同時刷。

 

上面是對初始串與目標串完全不同的情況,

如果有部分的不同:

ans[i]表示將str1[0...i]刷成str2[0...i]的最小步數,

if  str1[i]==str2[i]  則ans[i]=ans[i-1];

else

   ans[i]=min(ans[i],ans[j]+dp[j+1][i])  j<i;

 

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int dp[105][105],ans[105];//dp表示從i到j刷的次數,ans表示從0到i刷的次數。int main(){    int k,i,j,g,len;    char str1[100],str2[100];    while(~scanf("%s%s",str1,str2))          {               len=strlen(str1);              //上面的初始化不行,下面初始化可以,我不太清楚原因,但是我在想初始化一定伴隨更新。              /*for(j=0;j<len;j++)                for(i=j;i>=0;i--)                    {                     dp[i][j]=j-i+1;//初始化,每個字母都刷一遍。                   }*///這個是沒用的初始化。              for(k=0;k<len;k++)                for(i=0;i<len-k;i++)                 {                      j=i+k;//控制i,j區間。                     dp[i][j]=dp[i+1][j]+1;//初始化,每個字母都刷一遍,先每個單獨刷                         for(g=i+1;g<=j;g++)//i到j中間所有的刷法                           if(str2[g]==str2[i])//刷i的時候可以與k同時刷。                          dp[i][j]=min(dp[i][j],dp[i+1][g]+dp[g+1][j]);                          //i與k相同,尋找i刷到k的最優方案                   }                for(i=0;i<len;i++)                   {                        ans[i]=dp[0][i];//根據ans的定義先初始化                      }                for(i=0;i<len;i++)                  {                      if(str1[i]==str2[i])                          ans[i]=ans[i-1];//如果對應位置相等,這個位置可以不刷                        else                         {                            for(j=0;j<=i;j++)                            ans[i]=min(ans[i],ans[j]+dp[j+1][i]);//尋找j來分割區間得到最優解                         }                  }            printf("%d\n",ans[len-1]);          }    return 0;}/*zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.