HDU 2476 String painter (interval dp)

Source: Internet
Author: User

Test instructions: Given two strings, let the minimum number of changes change from the first string to the second string

Idea: Interval DP, directly consider two strings of words too difficult, only consider the second string, to change from the blank string to the second string of the minimum number of times, dp[i][j] I->j This interval of the optimal solution, then dp[i][j] = min (dp[i + 1][j), Dp[i + 1][k] + Dp[k + 1][j]), this state transfer equation is the position of the enumeration K, provided that the second string of the first character must be equal to the K-character, because this is the optimal, otherwise, there is no need to brush the position of K. At the end of each position enumeration, see if the first string is equal to the corresponding position of the second string, and find the optimal solution.

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN = the;intDP[MAXN][MAXN], ANS[MAXN];CharA[MAXN], B[MAXN];intMain () { while(~SCANF ("%s%s", A +1, B +1))    {        intLen = strlen (A +1); Memset (DP,0,sizeof(DP));  for(inti = len; I >=1; i--)        {             for(intj = i; J <= Len; J + +) {Dp[i][j]= Dp[i +1][J] +1;  for(intK = 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(inti =1; I <= Len; i++) Ans[i]= dp[1][i];  for(inti =1; I <= Len; i++)        {            if(A[i] = =B[i]) ans[i]= Ans[i-1]; Else//if not, just enumerate each position and find a minimum to            {                 for(intj =1; J < I; J + +) Ans[i]= Min (Ans[i], ans[j] + dp[j +1][i]); }} printf ("%d\n", Ans[len]); }    return 0;}
View Code

HDU 2476 String painter (interval dp)

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.