#
# Longest Common Subsequence
#
# Adrian Statescu <mergesortv@gmail.com>
#
# License MIT
import sys
def main():
x = [0,1,5,7,5,4]
y = [0,2,1,2,3,4,5,2,7]
n, m = len(x), len(y)
lcs = [ [0 for i in range(m)] for j in range(n) ]
#for debug
#print lcs
for i in range(1,n):
for j in range(1, m):
if x[i] == y[j]:
lcs[i][j] = 1 + lcs[i-1][j-1]
else:
lcs[i][j] = max(lcs[i][j-1], lcs[i-1][j])
print lcs[n-1][m-1]
i = n - 1
j = m - 1
ans = []
while i != 0 and j!= 0:
if(x[i] == y[j]):
ans.append(x[i])
i = i - 1
j = j - 1
else:
if lcs[i][j-1] < lcs[i-1][j]:
i = i - 1
else:
j = j - 1
while len(ans) != 0:
x = ans.pop()
sys.stdout.write(str(x) + " ")
print ""
main()
IwojIExvbmdlc3QgQ29tbW9uIFN1YnNlcXVlbmNlCiMKIyBBZHJpYW4gU3RhdGVzY3UgPG1lcmdlc29ydHZAZ21haWwuY29tPgojCiMgTGljZW5zZSBNSVQKCmltcG9ydCBzeXMKCmRlZiBtYWluKCk6CiAgICB4ID0gWzAsMSw1LDcsNSw0XQogICAgeSA9IFswLDIsMSwyLDMsNCw1LDIsN10KICAgIG4sIG0gPSBsZW4oeCksIGxlbih5KQogICAgbGNzID0gWyBbMCBmb3IgaSBpbiByYW5nZShtKV0gZm9yIGogaW4gcmFuZ2UobikgXQoKICAgICNmb3IgZGVidWcKICAgICNwcmludCBsY3MKICAgIAogICAgZm9yIGkgaW4gcmFuZ2UoMSxuKToKICAgICAgICBmb3IgaiBpbiByYW5nZSgxLCBtKToKICAgICAgICAgICAgaWYgeFtpXSA9PSB5W2pdOgogICAgICAgICAgICAgICAgbGNzW2ldW2pdID0gMSArIGxjc1tpLTFdW2otMV0KICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIGxjc1tpXVtqXSA9IG1heChsY3NbaV1bai0xXSwgbGNzW2ktMV1bal0pCiAgICBwcmludCBsY3Nbbi0xXVttLTFdCgogICAgaSA9IG4gLSAxCiAgICBqID0gbSAtIDEKICAgIGFucyA9IFtdCgogICAgd2hpbGUgaSAhPSAwIGFuZCBqIT0gMDoKICAgICAgICBpZih4W2ldID09IHlbal0pOgogICAgICAgICAgIGFucy5hcHBlbmQoeFtpXSkKICAgICAgICAgICBpID0gaSAtIDEKICAgICAgICAgICBqID0gaiAtIDEKICAgICAgICBlbHNlOgogICAgICAgICAgICBpZiBsY3NbaV1bai0xXSA8IGxjc1tpLTFdW2pdOgogICAgICAgICAgICAgICAgaSA9IGkgLSAxCiAgICAgICAgICAgIGVsc2U6CiAgICAgICAgICAgICAgICBqID0gaiAtIDEKICAgIHdoaWxlIGxlbihhbnMpICE9IDA6ICAgICAgICAgIAogICAgICAgICAgeCA9IGFucy5wb3AoKQogICAgICAgICAgc3lzLnN0ZG91dC53cml0ZShzdHIoeCkgKyAiICIpCgogICAgcHJpbnQgIiIKICAgICAgICAgICAgCm1haW4oKQ==