Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

Python: #dj 16D8101012J ito jun
n = int(input())
for _ 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):
            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])

Python: def lcs(X,Y):
  L = []
  for yk in Y:
    prev_idx = -1
    for l_idx, l in enumerate(L):
      x_idx = X.find(yk, prev_idx + 1)
      if x_idx < 0:
        break
      L[l_idx] = min(l, x_idx) 
      prev_idx = l
    else:
      x_idx = X.find(yk, prev_idx + 1)
      if x_idx >= 0:
        L.append(x_idx)

  return len(L)

def main():
  q = int(input())
  for _ in range(q):
    X = input()
    Y = input()
    print(lcs(X,Y))

if __name__ == "__main__":
  import sys, io
  main()

Python: def common(x, y):
    n = len(x)
    m = len(y)
    c1 = [0 for i in range(m + 1)]
    for i in range(n):
        e1 = x[i]
        c2 = c1[:]
        for j in range(m):
            if (e1 == y[j]):
                c1[j + 1] = c2[j] + 1
            elif (c1[j + 1] < c1[j]):
                c1[j + 1] = c1[j]
    return(c1[-1])


n = int(input())
for i in range(n):
    x = str(input())
    y = str(input())
    print(common(x, y))

Python: def lcs_better(X, Y):
    costs = [0]
    for c in Y:
        for i in range(len(costs) - 1, -1, -1):
            tmp = X.find(c, costs[i])
            if tmp + 1:
                if i + 1 < len(costs):
                    costs[i + 1] = min(costs[i + 1], tmp + 1)
                else:
                    costs.append(tmp + 1)
    return len(costs) - 1


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(lcs_better(X, Y))

Python: def solve(X, Y):
    dp = [[0] * (len(Y)+1)]
    for i in range(1,len(X)+1):
        dp.append([0])
        for j in range(1,len(Y)+1):
            if X[i-1] == Y[j-1]:
                dp[i].append(dp[i-1][j-1] + 1)
            else:
                dp[i].append(max(dp[i-1][j], dp[i][j-1]))
    return dp[len(X)][len(Y)]

num = int(input())
for i in range(num):
    X = input()
    Y = input()
    print(solve(X,Y))

Python: def lcs(X,Y):
    p=len(X)
    q=len(Y)
    Z=[[0 for i in range(q+1)]for j in range(p+1)]
    for i in range(p):
        for j in range(q):
            if X[i]==Y[j]:
                Z[i+1][j+1]=Z[i][j]+1
            else:
                if Z[i][j+1]>Z[i+1][j]:
                    Z[i+1][j+1]=Z[i][j+1]
                else:
                    Z[i+1][j+1]=Z[i+1][j]
    print(Z[p][q])

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

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 lcs(s1, s2):
    if len(s1) < len(s2):
      s1, s2 = s2, s1
    dp = []
    for s2_k in s2:
        #print(dp)
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):#要素とそのindexを取得
            #s2_kをs1のbgn_idx番目以降から探す
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            #print(s2_k, i, cur_idx, chr_idx, dp)
            if not chr_idx:#0だったらTrue#findは見つからないとき - 1を返す
                break
            dp[i] = min(cur_idx, chr_idx)
            #print(dp)
            bgn_idx = cur_idx
            
        else:#breakされなければ読み込まれる。
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if chr_idx:#0だったらfalse
                dp.append(chr_idx)
            #print(dp)    
             
    return len(dp)


n = int(input())
for _ in range(n):
    x = input()
    y = input()
    print(lcs(x, y))

Python: 



def lsc(str1,str2):
    table1=[0]*(len(str2)+1)
    for i in range(len(str1)):
       table2=table1[:]
       for j in range(len(str2)):
            if(str1[i] == str2[j]):
               table1[j+1] = table2[j] +1
            else:
                if(table1[j+1] < table1[j]):
                    table1[j+1]=table1[j]
    return table1[-1]




n=int(input())

for loop in range(n):
    str1=input()
    str2=input()
    print(lsc(str1,str2))

Python: n = int(input())
dataset = []
for i in range(n):
    tmp = []
    tmp.append(input())
    tmp.append(input())
    dataset.append(tmp)

for data in dataset:
    m = len(data[0])
    n = len(data[1])
    dp = []
    for x in range(n+1):
        dp.append([0] *(m+1))

    for j in range(n):
        for i in range(m):
            if data[0][i] == data[1][j]:
                dp[j+1][i+1] = dp[j][i] + 1
            else:
                #dp[j+1][i+1] = max(dp[j+1][i], dp[j][i+1])
                dp[J+1][i+1] = dp[j+1][i] if dp[j+1][i]>dp[j][i+1] else dp[j][i+1]
    print(dp[n][m])

Python: def len_LCS(X,Y):
    x_l = len(X)
    y_l = len(Y)
    lcs = [[0 for _ in range(x_l+1)] for _ in range(y_l+1)]
    for j in range(y_l):
        for i in range(x_l):
            if Y[j] == X[i]:
                lcs[j+1][i+1] = lcs[j][i] + 1
            else:
                lcs[j+1][i+1] = lcs[j+1][i] if lcs[j+1][i] > lcs[j][i+1] else lcs[j][i+1]
    return lcs[-1][-1]

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

Python: from sys import stdin

X = [ 0 for i in range(1000) ]
Y = [ 0 for i in range(1000) ]


def getLCS() :
	xlen = len(X[l])
	ylen = len(Y[l])
	Z = [[0 for i in range(xlen+2)]for i in range(ylen+2)]

	for i in range(1,xlen+1) :
		for j in range(1,ylen+1) :
			if X[i-1] == Y[j-1] : Z[i][j] = Z[i-1][j-1] + 1
			elif Z[i-1][j] >= Z[i][j-1] : Z[i][j] = Z[i-1][j]
			else : Z[i][j] = Z[i][j-1]

	return Z[xlen][ylen]

n = int(input())
for l in range(n) :
	X[l] = stdin.readline().strip()
	Y[l] = stdin.readline().strip()
	print(getLCS())