Codeforce 338 C skillfully deals with sequence matching problems

Source: Internet
Author: User

The problem is to give you two sequences a, and B, and then ask you to find a way to compose B with a minimum of Ade subsequence. For example a = ABC B = ABCCBA, then you can use two A for B, one is a positive sequence, the other is a negative sequence. For this problem we make a small conversion, we turn a inversion into A1, then we use the increment of a and A1 sequence to represent B, we use dp[2][i][j] to represent the length of the longest consistent sequence starting from a (A1) [I],b[j], and then solve the answer by using greedy method, See the code for details:

#include <cstdio>#include<algorithm>#include<cstring>#include<map>#include<vector>using namespacestd;Chars[2500], t[2500];intlens, lent;intdp[2][2500][2500];//s I,t J start the longest equal sequence of the backwardvector<pair<int,int> >Res;intMain () {scanf ("%s%s", S, t); Lens=strlen (s); Lent=strlen (t);  for(inti=lens-1; i>=0; i--)         for(intj=lent-1; j>=0; j--)        {            if(S[i]==t[j]) dp[0][I][J] = dp[0][i+1][j+1]+1; if(s[lens-1-I] = = T[j]) dp[1][I][J] = dp[1][i+1][j+1]+1; }    intCur =0;  while(Cur <lent) {        intTP =0;//the longest matching string starting from the cur positionpair<int,int> add;//Current Best Answers         for(intI=0; i<lens; i++)        {            if(dp[0][i][cur] >TP) {TP= dp[0][i][cur]; Add= Make_pair (i+1, i+TP); }            if(dp[1][i][cur] >TP) {TP= dp[1][i][cur]; //add = Make_pair (I+TP, i+1);Add = Make_pair (Lens-i, lens+1-i-TP); }        }        if(TP = =0) {printf ("-1\n"); return 0;        } res.push_back (add); Cur+=TP; } printf ("%d\n", Res.size ());  for(intI=0; I<res.size (); i++) {printf ("%d%d\n", Res[i].first, Res[i].second); }    return 0;}

Codeforce 338 C skillfully deals with sequence matching problems

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.