#P4584. [FJOI2015] 带子串包含约束LCS问题

[FJOI2015] 带子串包含约束LCS问题

Description

The longest common subsequence problem with substring inclusion constraints can be stated as follows.

Given two sequences XX and YY of lengths nn and mm, respectively, and a set SS of substring inclusion constraints.

There are kk strings in SS, S={S1,S2,,Sk}S=\{S_1,S_2,…,S_k\}, where the length of string SiS_i is lil_i, 1ik1 \le i \le k. The problem is to find a longest common subsequence of XX and YY that contains every string in the constraint set SS as its substring.

For example, if the given sequences are X=actaagacctX = \texttt{actaagacct} and Y=gacctacctcY = \texttt{gacctacctc}, and the constraint set is S={ata,tact}S=\{\texttt{ata}, \texttt{tact}\}, then the subsequence actacct is an unconstrained LCS of XX and YY, while a longest common subsequence that contains all strings in SS as substrings is atact. In this problem, please pay special attention to the difference between substring and subsequence. The substring of a string T=t1tnT=t_1…t_n is a string of the form TT=t1+itm+i=t_1+i…t_m+i, where 0i0 \le i, m+inm+i \le n. For example, if T=abcdefgT=\texttt{abcdefg}, then bcd is a substring of TT, while bce is a subsequence of TT but not a substring of TT.

Design an algorithm to find a longest common subsequence of the given sequences XX and YY that satisfies the substring inclusion constraints SS.

Input Format

  • The first line contains positive integers nn, mm, kk, with m<300m < 300, n<300n < 300, k<6k < 6. Here nn and mm are the lengths of the given sequences XX and YY, respectively. The value kk is the number of strings in the constraint set SS.
  • The second line contains kk integers lil_i, with 0li3000 \le l_i \le 300, 1ik1 \le i \le k, which are the lengths of the kk strings in the constraint set SS.
  • The third and fourth lines give the sequences XX and YY.
  • The next kk lines each contain one string SiS_i.

Output Format

Output the length of a longest common subsequence of XX and YY that satisfies the substring inclusion constraints SS.

10 10 2
3 4
actaagacct
gacctacctc
ata
tact
5

Hint

The strings contain only uppercase and lowercase letters.

Translated by ChatGPT 5