import sys


def LCS(a_len, b_len, a, b):
    Ans = [[0 for j in range(b_len+1)] for i in range(a_len+1)]

    for i, x in enumerate(a):
        for j, y in enumerate(b):
            if x == y:
                Ans[i+1][j+1] = Ans[i][j]+1
            else:
                Ans[i+1][j+1] = 0

    max_len = -1
    max_index = -1
    for i in range(a_len):
        for j in range(b_len):
            if Ans[i+1][j+1] > max_len:
                max_len = Ans[i+1][j+1]
                max_index = i+1

    return a[max_index-max_len:max_index]


def main():
    data = input()
    data = data.split()
    try:
        a_len = int(data[0])
        b_len = int(data[1])
        a_str = data[2]
        b_str = data[3]
        ans = LCS(a_len, b_len, a_str, b_str)
        if ans:
            print(ans)

    except:
        print('輸入必須以空白間隔\n請重新輸入')
        main()

if __name__ == '__main__':
    main()
    sys.exit()

@@ -551,20 +551,26 @@
 ():%0A    
-data
+str_length
  = input
@@ -580,19 +580,31 @@
     
-data = data
+str_length = str_length
 .spl
@@ -637,20 +637,26 @@
 n = int(
-data
+str_length
 %5B0%5D)%0A   
@@ -676,69 +676,118 @@
 int(
-data%5B1%5D)%0A        a_str = data%5B2%5D%0A        b_str = data%5B3%5D%0A    
+str_length%5B1%5D)%0A    except:%0A        print(%22%E5%AD%97%E4%B8%B2%E9%95%B7%E5%BA%A6%E6%87%89%E7%82%BA%E6%95%B4%E6%95%B8%E5%9E%8B%E6%85%8B%22)%0A        main()%0A    a_str = input()%0A    b_str = input()%0A
     
@@ -824,20 +824,16 @@
  b_str)%0A
-    
     if a
@@ -836,20 +836,16 @@
 if ans:%0A
-    
         
@@ -860,69 +860,8 @@
 s)%0A%0A
-    except:%0A        print('%E8%BC%B8%E5%85%A5%E5%BF%85%E9%A0%88%E4%BB%A5%E7%A9%BA%E7%99%BD%E9%96%93%E9%9A%94%5Cn%E8%AB%8B%E9%87%8D%E6%96%B0%E8%BC%B8%E5%85%A5')%0A        main()%0A
 %0Aif 

Modify input type