Go: package main

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

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: # coding=utf-8

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

q = int(input())

for _ in range(q):
    X = list(input())
    Y = list(input())
    c = [[0 for _ in range(len(Y))] for _ in range(len(X))]
    max_c = lcs(X, Y)
    print(max_c)

Python: def answer(X, Y):
    m, n = len(X), len(Y)
    C = [[0] * (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]:
                C[i][j] = C[i-1][j-1] + 1
            else:
                C[i][j] = max(C[i][j -1], C[i -1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(answer(X, Y))

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")