#P4608. [FJOI2016] 所有公共子序列问题

    ID: 3533 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串高精度2016各省省选福建

[FJOI2016] 所有公共子序列问题

Description

A subsequence of a given sequence is obtained by deleting some elements from that sequence. Precisely, given a sequence X=x1x2xmX = x_1 x_2 \ldots x_m, another sequence Z=z1z2zkZ = z_1 z_2 \ldots z_k is a subsequence of XX if there exists a strictly increasing index sequence i1,i2,,iki_1, i_2, \ldots, i_k such that for all j=1,2,,kj = 1, 2, \ldots, k we have zj=xijz_j = x_{i_j}.

For example, the sequence Z=GACTZ = ``GACT'' is a subsequence of the sequence X=GCTACTX = ``GCTACT'', with the corresponding increasing index sequence 1,4,5,61, 4, 5, 6. Given two sequences XX and YY, when another sequence ZZ is a subsequence of both XX and YY, we call ZZ a common subsequence of XX and YY. For example, if X=GCTACTX = ``GCTACT'' and Y=GATCCTY = ``GATCCT'', the sequence T'' is a common subsequence of $X$ and $Y$, and the sequence GACT'' is also a common subsequence of XX and YY. Note that for any given sequences XX and YY, the empty sequence is always their common subsequence.

The All Common Subsequences problem asks you, given two sequences X=x1x2xmX = x_1 x_2 \ldots x_m and Y=y1y2ynY = y_1 y_2 \ldots y_n, to find all distinct common subsequences of XX and YY.

Input Format

The first line contains two positive integers mm and nn, the lengths of the two input sequences XX and YY.

The next two lines give the input sequences X=x1x2xmX = x_1 x_2 \cdots x_m and Y=y1y2ynY = y_1 y_2 \cdots y_n, where each element of a sequence is an English letter, uppercase or lowercase.

The last line contains a non-negative integer kk.

If k=1k = 1, you must output all distinct common subsequences of XX and YY, and also how many distinct common subsequences XX and YY have.

If k=0k = 0, you must output only how many distinct common subsequences XX and YY have.

Output Format

Output all distinct common subsequences of XX and YY, or output how many distinct common subsequences XX and YY have. When k=1k = 1, first output all distinct common subsequences of XX and YY, one per line, in ascending lexicographic order. Finally, output the number of distinct common subsequences. When k=0k = 0, output only the number of distinct common subsequences.

6 6
GCTACT
GATCCT 1
A
AC
ACT
AT 
C  
CC 
CCT
CT 
G  
GA 
GAC
GACT
GAT 
GC  
GCC 
GCCT
GCT 
GT  
GTC 
GTCT
GTT 
T   
TC  
TCT 
TT  
26

Hint

1m,n30101 \leq m, n \leq 3010.

The answer can be very large.

Translated by ChatGPT 5