Hdu2476string painter (interval DP)

Source: Internet
Author: User
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 STR Ing 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
zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd

Sample output
67. Given two strings a and B, evaluate the minimum number of operations required for a to change a to B. During each operation, any part of a is converted into any letter.
#include<stdio.h>#include<string.h>int min(int a,int b){    return a>b?b:a;}int main(){    int dp[105][105],ans[105];    char a[105],b[105];    while(scanf("%s%s",a,b)>0)    {        int len=strlen(a);        memset(dp,0,sizeof(dp));        for(int r=0;r<len;r++)        for(int i=0;i<len-r;i++)        {            int j=i+r;            dp[i][j]=dp[i+1][j]+1;            for(int k=i+1;k<=j;k++)            if(b[i]==b[k])            dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);        }        for(int i=0;i<len; i++)        ans[i]=dp[0][i];        for(int i=0;i<len; i++)        if(a[i]==b[i])        {            if(i==0)ans[i]=0;            else ans[i]=ans[i-1];        }        else{            for(int k=0;k<i;k++)            ans[i]=min(ans[i],ans[k]+dp[k+1][i]);        }        printf("%d\n",ans[len-1]);    }}


Hdu2476string painter (interval DP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.