Yet Another Blog

Maximum number of crossed matchings between two sequences

leave a comment »

Back to OJ problems …

Problem

PKU – 1692

Given two sequences of integers,A=a_1,a_2,a_3...andB=b_1,b_2,b_3..., we are allowed to match an integera_ifrom the first sequence with an integerb_jfrom the second sequence only ifa_i=b_j.  No integer in either sequence can be matched with more than one integer in the other sequence.

Each matching must cross with another one and only one different matching. Two matchings(a_i,b_j)and(a_k,b_l)are considered crossed iffi<kandl<j, or ifk<iandj<l. The two matchings are considered different iffa_i\neq a_k.

We are required to know the maximum number of crossed matchings that can be done.  For example, in the following figure, the maximum is 4.

Solution

The key to solve this problem is to notice its similarity to the well-known longest common subsequence (LCS) problem. However, while the LCS problem asks for the maximum number of uncrossed matchings, this problem asks for the maximum number of crossed matchings.

LetA_mbe a sequence that contains the firstmelements ofAin order. Similarly,B_nis a sequence that contains the firstnelements ofBin order. LetMCM(A_m,B_n)be the maximum number of crossed matchings between the two sequencesA_mandB_n. We define the the predicateboundary(A_m,B_n)to be true if and only if there is a crossed matching betweenA_mandB_nthat involves the last element ofA_mand the last element ofB_n; that is,

boundary(A_m,B_n) \Leftrightarrow\ a_m\neq b_n\wedge (\exists\ i<m\ \exists\ j<n\ (a_m=b_j\wedge b_n=a_i))

The importance of this predicate is that it determines whether adding/dropping the last element inA_morB_nwill add/drop a crossed matching. If the predicateboundary(A_m,B_n)is satisfied, then we are interested to know what elements are involved in this boundary crossed matching. Letl\_idx(A_m, b_n)denote the index of the last occurrence of b_ninA_m; that is,l\_idx(A_m,b_n)=\displaystyle\max\{i:i<m\ and\ a_i=b_n\}. Similarly,l\_idx(B_n,a_m)=\displaystyle\max\{j:j<n\ and\ b_j=a_m\}is the index of the last occurrence ofa_minB_n. If we are going to count this boundary crossed matching in our maximal set of crossed matchings, then we will have to uncount any other crossed matchings that involve elements betweenl\_idx(A_m,b_n)anda_minclusive, or elements betweenl\_idx(B_n,a_m)andb_ninclusive. This effectively means going back toMCM(A_{l\_idx(A_m,b_n)-1},B_{l\_idx(B_n,a_m)-1}). Therefore we shouldn’t count a boundary crossed matching unless there are no other crossed matchings that will be uncoutned for this purpose. We defineMCMrecursively as follow:

Function MCM(A_m,B_n)
\ If m=0orn=0 Then Return 0
\ res\leftarrow max(MCM(A_m,B_{n-1}),MCM(A_{m-1},B_n))
\ If boundary(A_m,B_n) Then
\ \ res\leftarrow\max(res, MCM(A_{l\_idx(A_m,b_n)-1},B_{l\_idx(B_n,a_m)-1})+1)
\ Return res

which is quite similar to the recursive solution of the LCS problem. The problem can then be solved efficiently using dynamic programming.

Advertisement

Written by Hatem Abdelghani

March 23, 2010 at 6:15 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.