Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			if x[i] == y[j] {
				c[i][j] = c[i-1][j-1] + 1
			} else {
				c[i][j] = max(c[i-1][j], c[i][j-1])
			}
			maxl = max(maxl, c[i][j])
		}
	}
	return maxl
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

Python: # -*- coding: utf-8 -*-


if __name__ == '__main__':

    
    def lcs(X, Y):
        m = len(X)
        n = len(Y)
        c = [[-1 for i in range(n+1)] for j in range(m+1)]
        X = " " + X
        Y = " " + Y
        for i in range(m+1):
            c[i][0] = 0
        for j in range(1, n+1):
            c[0][j] = 0
        for i  in range(1, m+1):
            for j in range(1, n+1):
                if X[i] == Y[j]:
                    c[i][j] = c[i-1][j-1] + 1
                else:
                    c[i][j] = max(c[i-1][j], c[i][j-1])
        return c[m][n]

    def lcs2(X, Y):
        n = len(Y)
        dp = [0]*(n+1)
    
        for c in X:
            tmp = dp[:]
            for j in range(n):
                if c == Y[j]:
                    dp[j+1] = tmp[j] + 1
                elif dp[j+1] < dp[j]:
                    dp[j+1] = dp[j]
    
        return dp[-1]

    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(lcs2(X,Y))

Python: def lcs(s1,s2):
    l1=len(s1)
    work=[0]*(l1+1)

    #enumerateを使って文字列へのアクセスを極力避ける
    for x in s2:
        work_old=work[:]
        for j,y in enumerate(s1):
            if x==y :
                work[j+1]=work_old[j]+1
            elif work[j]>work_old[j+1]:
                work[j+1]=work[j]
                
    print(work[-1])


import sys
input=sys.stdin.readline


q=int(input())
for _ in range(q):
    lcs(input()[:-1],input()[:-1])

Python: q = int(input())
ans = []

for i in range(q):
    s1 = input()
    n1 = len(s1)
    s2 = input()
    n2 = len(s2)

    f = [[0 for j in range(n2)] for i in range(n1)]
    if s1[0] == s2[0]:
        f[0][0] = 1
    else:
        f[0][0] = 0

    for j in range(1, n2):
        if f[0][j-1] == 0:
            if s1[0] == s2[j]: delta = 1
            else: delta = 0
            f[0][j] = f[0][j-1]+delta
        else:
            f[0][j] = f[0][j-1]

    for i in range(1, n1):
        if f[i-1][0] == 0:
            if s1[i] == s2[0]: delta = 1
            else: delta = 0
            f[i][0] = f[i-1][0]+delta
        else:
            f[i][0] = f[i-1][0]

    for i in range(1, n1):
        for j in range(1, n2):
            if s1[i] == s2[j]:
                f[i][j] = f[i-1][j-1]+1
            else:
                f[i][j] = max(f[i-1][j], f[i][j-1])

    ans.append(f[n1-1][n2-1])
    #print(f)

for a in ans:
    print(a)

Python: q = int(input())
for _ in range(q):
    x = input()
    y = input()
    m = len(x)
    n = len(y)
    c = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
    for i in range(1, m + 1):
        tmp = x[i - 1]
        for j in range(1, n + 1):
            if tmp == y[j - 1]:
                c[i][j] = c[i - 1][j - 1] + 1
            elif c[i - 1][j] >= c[i][j - 1]:
                c[i][j] = c[i - 1][j]
            else:
                c[i][j] = c[i][j - 1]
    print(c[m][n])

Python: #coding:UTF-8
def LCS(X,Y):
    count=0
    xlist=list(X)
    ylist=list(Y)
    anslist=[]
    ynum=0
    xnum=0
    yflag=False
    for i in range(len(ylist)):
        for j in range(i,len(xlist)):
            anslist.append([j,i])
    for i in range(len(anslist)):
        if xlist[anslist[i][0]]==ylist[anslist[i][1]] and ynum==anslist[i][1] and anslist[i][0]>xnum:
            count+=1
            yflag=True
            ynum+=1
            xnum=anslist[i][0]
        if anslist[i][0]==len(xlist)-1:
            if yflag==True:
                yflag=False
            else:
                ynum+=1
    print(count)
if __name__=="__main__": 
    n=int(input())
    for i in range(n):
        X=input()
        Y=input()
        LCS(X,Y)

Python: import sys

def dp_init(m, n):
    ret = []
    for i in range(m):
        ret.append([None] * n)
    return ret

def solve(str_a, str_b):
    dp = dp_init(len(str_a), len(str_b))
    return rec(len(str_a), len(str_b), str_a, str_b, dp, 0)

def rec(i, j, str_a, str_b, dp, depth):
    if dp[i-1][j-1] is not None:
        #print(dp)
        #print(" "*depth + "(%d, %d) : %d - hit" % (i-1, j-1, dp[i-1][j-1]))
        return dp[i-1][j-1]
    if i-1 == 0 or j-1 == 0:
        dp[i-1][j-1] = 0
        #print(" "*depth + "(%d, %d) : %d - zero" % (i-1, j-1, dp[i-1][j-1]))
        return 0
    if str_a[i-1] == str_b[j-1]:
        dp[i-1][j-1] = rec(i-1, j-1, str_a, str_b, dp, depth+1) + 1
        #print(" "*depth + "(%d, %d) : %d" % (i-1, j-1, dp[i-1][j-1]))
        return dp[i-1][j-1]

    dp[i-1][j-1] = max(rec(i, j-1, str_a, str_b, dp, depth+1), rec(i-1, j, str_a, str_b, dp, depth+1))
    #print(" "*depth + "(%d, %d) : %d" % (i-1, j-1, dp[i-1][j-1]))
    return dp[i-1][j-1]

nb_pairs = int(input())
pairs = []
for i in range(0, nb_pairs):
    pairs.append((input(), input()))

for i in range(0, nb_pairs):
    str_a, str_b = pairs[i]
    print(solve(str_a, str_b))

Python: from sys import stdin
input = stdin.readline

q = int(stdin.readline())

def solve():
    xs = input()
    ys = input()
    len_x = len(xs)-1
    len_y = len(ys)-1
    # [i,j]までで一番長い数値とする
    dp = [0] * (len_x+1)
    memo = [0] * (len_x+1)

    for j in range(len_y):
        yj = ys[j]
        # 参照コピーにならない
        memo = dp[:]
        for i in range(len_x):
            if xs[i] == yj:
                dp[i+1] = memo[i] + 1
            elif dp[i+1] < dp[i]:
                dp[i+1] = dp[i]
    return dp[len_x]


ret = [ solve() for i in range(q)]
print(*ret, sep="\n")

Python: def m():
 e=input
 a=''
 for _ in[0]*int(e()):
  X,z=e(),[]
  for y in e():
   s=i=0
   for k in z:
    t=X.find(y,s)+1
    if t<1:break
    if t<k:z[i]=t
    s=k;i+=1
   else:
    t=X.find(y,s)+1
    if t:z+=[t]
  a+=f'\n{len(z)}'
 print(a[1:])
if'__main__'==__name__:m()

Python: def lcs(X, Y):
    m, n = len(X), len(Y)
    memo = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                memo[i][j] = memo[i - 1][j - 1] + 1
            else:
                memo[i][j] = max(memo[i][j - 1], memo[i - 1][j])
    return memo[m][n]

n = int(input())
for _ in range(n):
    X, Y = input(), input()
    print(lcs(X, Y))

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())
for _ in range(n):
    x = input()
    y = input()
    print(lcs(x, y))