Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"bufio"
	"strings"
	"sort"
)

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

Python: import sys
import functools as ft
sys.setrecursionlimit(10000)

q = int(input())
X = ""
Y = ""
memo = [[]]

def print_memo() :
    for x in range(len(X)+1):
        print(ft.reduce(lambda x,y: x + str(y) + "\t" ,memo[x], ""))

def LCS(x, y):
    if (x < 0) or (y < 0):
        return 0
    elif memo[x][y] != -1:
        return memo[x][y]
    elif X[x] == Y[y]:
        memo[x][y] =  LCS(x-1, y-1) + 1
        return memo[x][y]
    else:
        memo[x][y] = max(LCS(x-1,y), LCS(x,y-1))
        return memo[x][y]


for c in range(q):
    X = input()
    Y = input()
    len_x = len(X)
    len_y = len(Y)
    memo = [[0 for j in range(len_y+1)] for i in range(len_x+1)]

    for x in range(1, len_x+1):
        for y in range(1, len_y+1):
            memo[x][y] = max(memo[x-1][y-1] + int(X[x-1]==Y[y-1]), memo[x-1][y], memo[x][y-1])

    print(memo[len_x][len_y])
    #print_memo()

Python: def main():
    n = int(input())
    result = []
    for i in range(n):
        a = input()
        b = input()
        array = [[1 for i in range(len(b))] for j in range(len(a))]
        for i in range(1, len(a)):
            for j in range(1, len(b)):
                c = 0
                if a[i] == b[j]:
                    c = 1
                array[i][j] = max(
                                    array[i-1][j],
                                    array[i][j-1],
                                    array[i-1][j-1]+c)
        result.append(array[len(a)-1][len(b)-1])
    for i in result:
        print(i)
if __name__ == "__main__":
    main()

Python: def f(i,j):
    if i == 0 or j == 0:
        if s1[i] == s2[j]:
            x[i][j] = 1
        else:
            if i == 0:
                x[i][j] = x[i][j-1]
            else:
                x[i][j] = x[i-1][j]
    elif s1[i] == s2[j]:
        x[i][j] = max(x[i-1][j-1]+1,x[i][j-1],x[i-1][j])
    else:
        x[i][j] = max(x[i-1][j-1],x[i][j-1],x[i-1][j])

n = int(input())
for i in range(n):
    s1 = input()
    s2 = input()
    x = [[0 for j in range(len(s2))]for k in range(len(s1))]
    for j in range(len(s1)):
        for k in range(len(s2)):
            f(j,k)
            
    print(x[len(s1)-1][len(s2)-1])

Python: 
def main():
    n = int(input())
    for i in range(n):
        a = input()
        b = input()
        na=len(a)
        nb=len(b)
        array = [[0 for i in range(nb)] for j in range(na)]
        for i in range(na):
            for j in range(nb):
                if i==0:
                    if a[i]==b[j]:
                        array[i][j]=1
                elif j==0:
                    if a[i]==b[j]:
                        array[i][j]=1
                else:
                    c = 0
                    if a[i] == b[j]:
                        c = 1
                    array[i][j] = max(
                                        array[i-1][j],
                                        array[i][j-1],
                                        array[i-1][j-1]+c)
        print(array[na-1][nb-1])
if __name__ == "__main__":
    main()

Python: def lcs(s1, s2):
    dp = []
    for s2_k in s2:
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if not chr_idx:
                break
            dp[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        else:
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if chr_idx:
                dp.append(chr_idx)
    return len(dp)

n = int(input())
ans = []
for _ in range(n):
    x = input()
    y = input()
    ans.append(lcs(x, y))

for i in ans:
    print(i)

Python: def len_LCS(X,Y):
    Z = X
    if len(X) > len(Y): Z,Y = Y,X
    D = {}
    def len_LCS_core(Z,Y):
        if Z in D: return D[Z]
        if Z in Y:
            D[Z] = len(Z)
            return D[Z]
        D[Z] = 0
        if len(Z) < 2: return 0
        for i in range(len(Z)):
            D[Z] = max(D[Z], len_LCS_core(Z[:i]+Z[i+1:],Y))
        return D[Z]
    return len_LCS_core(Z,Y)
        
if __name__=='__main__':
    n = int(input())
    for _ in range(n):
        X = input()
        Y = input()
        print(len_LCS(X,Y))

Python: if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    N = 1000

    def lcs(X, Y):
        c = [[0]*(N+1) for _ in range(N+1)]
        maxl = 0
        X = ' ' + X # 1オリジンにする
        Y = ' ' + Y
        pre_row = c[0]
        for i, x in enumerate(X):
            if i == 0:
                continue
            row = c[i]
            for j, y in enumerate(Y):
                if j == 0:
                    continue
                if x == y:
                    value = pre_row[j-1] + 1
                    row[j] = value
                    if maxl < value: # maxlの更新はこの時しか起こらない
                        maxl = value
                else:
                    pre_row_j = pre_row[j]
                    row_j_1 = row[j-1]
                    value = row_j_1
                    if pre_row_j > row_j_1:
                        value = pre_row_j # max関数だと、とても遅い
                    row[j] = value
            pre_row = row # 今の列が次の列にとって一つ前の列になる

        return maxl

    n = int(input())
    for _ in range(n):
        s1 = input()[:-1]
        s2 = input()[:-1]
        print(lcs(s1, s2))

Python: def Ics(X, Y):
    m = len(X)
    n = len(Y)
    c1 = [0] * (n + 1)
    c2 = [0] * (n + 1)
    for i in range(m):
        d = X[i]
    for j in range(n):
        if (d == Y[j]):
            c1[j + 1] = c2[j] + 1
        elif (c1[j + 1] < c1[j]):
            c1[j + 1] = c1[j]
    return (c1[-1])

q = int(input().rstrip())
t = []
for i in range(q):
    s1 = input().rstrip()
    s2 = input().rstrip()
    t.append(Ics(s1, s2))
print(*t, sep="\n")