hdu2476--string painter (interval dp)

Source: Internet
Author: User

Description there is strings A and B with equal length. Both strings is 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 of all other character you want. That's, after using the painter, the segment are made up for only one kind of character. Now the your task is to the change A to B using string painter. What ' s the minimum number of operations?

Input input contains multiple cases. Each case consists of lines:
The first line contains string A.
The second line contains string B.
The length of both strings would not be greater than 100.

Output A single line contains one integer representing the answer.

Sample Input

Zzzzzfzzzzz
abcdefedcba
abababababab
CDCDCDCDCDCD

Sample Output

6
7

It's a little hard to think about.

It has to be divided into two parts to solve this problem.

DP[I][J] Indicates the number of steps required to brush an empty string (or a string completely different from the target string) into the target string.

We look for the characters in the middle of the target string, and if it's the same as this, it's the equivalent of swiping the string header while brushing it.

Then the res is obtained based on the first step result.



#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector > #include <queue> #include <set> #include <map> #include <string> #include <cmath> #inc
Lude <cstdlib> #include <iostream> using namespace std;
const int MAXN = 310;
const int INF = 1000000007;
const int MOD = 1000000007;
const double eps=1e-8;

Const double PI = ACOs (-1);
int DP[MAXN][MAXN];
int ANS[MAXN];
String A, B;
        int main () {while (cin>>a>>b) {int n=a.length ();
        Memset (Dp,0,sizeof (DP));
        memset (ans,0,sizeof (ans));
                for (int k=0;k<n;k++) {for (int i=0;i+k<n;i++) {int j=i+k;
                Dp[i][j]=min (dp[i+1][j]+1,dp[i][j-1]+1); for (int m=i+1;m<=j;m++) {if (B[m]==b[i]) {dp[i][j]=min (dp[i][j],dp[i+1][m]+d
                    P[M+1][J]);
       }
                }
            }
        } for (int i=0;i<n;i++) ans[i]=dp[0][i];
            for (int i=0;i<n;i++) {if (A[i]==b[i]) ans[i]=ans[i-1];
                else{for (int j=0;j<i;j++) {ans[i]=min (ans[j]+dp[j+1][i],ans[i]);
    }}} printf ("%d\n", ans[n-1]);
 }
}








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.