Uva-1625-color Length (DP LCS variant)

Source: Internet
Author: User
Tags first string

Color Length (UVA-1625) (DP LCS variant) topic

Enter a color sequence of two lengths of N,m (<5000) respectively. Requires that the same sequence be synthesized sequentially, i.e. the color at the beginning of a sequence can be placed at the end of a new sequence at a time.

Https://odzkskevi.qnssl.com/a68cbd3e27f46b4f02ea12b7b1a1abca

The resulting new sequence, for each color C, has a position where L (c) represents the difference between the minimum position and the maximum position, and a new sequence of the minimum sum of L (c) is obtained.

Analysis
    • LCS is a public ascending subsequence, in the process of dynamic transfer, consider the first sequence of I, and the second sequence of J, if I and J are the same, then dp[i][j]=d[i-1][j-1]+1 , if not the same, then dp[i ][j]= Max (dp[i-1][j],dp[i][j-1]) . Complete the dynamic transfer.
    • and the problem is to make L (c) the smallest, in the process of dynamic transfer, we want to record the value of L (c) and also to consider whether there is a color start or end. In order to transfer L (c) and, we also use an array C to record how many colors are currently starting but not yet finished. (Thinking purple potato is very clear)
    • dp[i][j] = min (dp[i-1][j]+c[i-1][j],dp[i][j-1]+c[i][j-1]) I,j represents the first I and the first J constitute a new string. The
    • approach is to preprocess the first occurrence and end occurrences of each character in a two string. Then DP. DP State transfer ideas are not difficult to think about, but need some new details to consider.
    • summarizes some details
      • because there can be one string that has not been involved in the composition of the two strings, and the other string has been synthesized. So the DP array subscript 0 is the meaning, but if the string is also starting from subscript 0, is it a bit troublesome? So the first detail is that the string starts with subscript 1. The
      • can initialize i = 0, but it is better to handle it in a loop for the sake of wholeness. (preprocessing can only handle the first 0, only the second string, but in the DP process, each layer is processed to take the first string alone, the second string is 0, so it is better to put in the loop directly to consider)
const int MAXN = 5000+5;const int INF = 1000000000;char p[maxn],q[maxn];int sp[26],sq[26],ep[26],eq[26];int D[maxn],c[max n];//Other people's blogs seem to be two-story, but in fact one layer is enough.    int main () {int t;cin>>t;        while (t--) {scanf ("%s%s", p+1,q+1);        int n = strlen (p+1), M = strlen (q+1);        for (int i=1;i<=n;i++) p[i]-= ' A ';        for (int j=1;j<=m;j++) q[j]-= ' A ';            for (int i=0;i<26;i++) {Sp[i] = sq[i] = INF;        Ep[i] = Eq[i] = 0;        }//get to this preprocessing, which has not been encountered before.            for (int i=1;i<=n;i++) {Sp[p[i]] = min (sp[p[i]],i);        Ep[p[i]] = i;            } for (int i=1;i<=m;i++) {Sq[q[i]] = min (sq[q[i]],i);        Eq[q[i]] = i;        } memset (c,0,sizeof c); memset (d,0,sizeof D); for (int i=0;i<=n;i++) {for (int j = 0;j<=m;j++) {if (!J&AMP;&AMP;!I) cont                inue;//two are 0, go straight ahead. int v1=inf,v2 = inf;//for this special case, only two values are first stored in v1,v2, and the assignment is INF。                if (i) v1=d[j]+c[j];                if (j) v2=d[j-1]+c[j-1];                D[j] = min (v1,v2);                    if (i) {c[j] = c[j]; If the conditions in the IF (sp[p[i]]==i&&sq[p[i]]>j) c[j]++;//if, here the details also need to think about if (ep[p[i]]==i&&eq[p[i]]<                =J) c[j]--;                    } else if (j) {c[j] = c[j-1];                    if (sq[q[j]] = = J && sp[q[j]]>i) c[j]++;                if (eq[q[j]] = = J && ep[q[j]]<=i) c[j]--;    }}} printf ("%d\n", D[m]); } return 0;}

Uva-1625-color Length (DP LCS Warp)

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.