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