Go: package main

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

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

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = " " + s1 , " " + s2
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

Python: import sys
def lcs(X, Y):
    x = len(X) + 1
    y = len(Y) + 1
    c = [0 for y in range(y)]

    for i in X:
        tmp = c[:]
        for ji, yc in enumerate(Y):
            j = ji + 1
            if i == yc:
                c[j] = tmp[j - 1] + 1
            else:
                c[j] = max(tmp[j], c[j - 1])
    return c[y - 1]

n = int(input())

for i in range(n):
    X = sys.stdin.readline()[:-1]
    Y = sys.stdin.readline()[:-1]
    print(lcs(X, Y))

Python: """
ALDS1_10_C
最長共通部分列を二次元配列を用いた動的計画法で求める
だた，普通に二次元配列を構成するとTLEになるので工夫しなければならない
IKAPPERさんのコードを参考にしました
"""

def lcs(X, Y):
    m = len(X)
    n = len(Y)
    #xが縦，yが横
    lcs_r = [0 for _ in range(m+1)]
    for y in Y:
        previous = lcs_r[:] #copy.copy 使わなくて済むかも
        for i, x in enumerate(X):
            if x == y:
                lcs_r[i+1] = previous[i]+1
            elif lcs_r[i] > previous[i]:
                lcs_r[i+1] = lcs_r[i]
            else: #ここは省略できる（そのままpreviousがlcs_rに上がってくる）
                lcs_r[i] = previous[i] 
    
    print(lcs_r[-1])



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

Python: def main():
    num = int(input())
    for i in range(num):
        X = " " + input()
        Y = " " + input()
        lx = len(X)
        ly = len(Y)
        T = [0] * ly
        for j in range(1,lx):
            C = T[:]
            for k in range(1,ly):
                k1 = k-1
                if X[j] == Y[k]:
                    T[k] = C[k1] + 1
                elif C[k] > T[k1]:
                    T[k] = C[k]
                else:
                    T[k] = T[k1]
        print(T[k])

if __name__ == '__main__':
    main()

Python: import numpy as np

Python: def findlcs(x, y):
    dp = [0]*(len(y)+1)

    for c in x:
        tmp = dp[:]
        for j in range(len(y)):
            if c == y[j]:
                dp[j+1] = tmp[j] + 1
            else:
                dp[j+1] = max(dp[j+1], dp[j])

    return dp[-1]


q = int(input())
for k in range(q):
    X = input()
    Y = input()
    print(findlcs(X, Y))

Python: q = int(input())
d = [[0 for _ in range(1001)] for _ in range(1001)]
for i in range(q):
    x = input()
    y = input()
    n = len(x)
    m = len(y)
    for i in range(1001):
        d[i][0]=0
        d[0][i]=0
    for i in range(n):
        for j in range(m):
            if x[i] == y[j]:
                d[i+1][j+1] = d[i][j]+1
            else:
                d[i+1][j+1] = max(d[i+1][j],d[i][j+1])
    print(d[n][m])

Python: def lcs(str1,str2):
    f=[[0 for i in range(len(str2)+1)]for j in range(len(str1)+1)]
    for i in range(1,len(str1)+1):
        for j in range(1,len(str2)+1):
           if str1[i-1]==str2[j-1]:
              f[i][j]=f[i-1][j-1]+1
           else:
              f[i][j]=max(f[i-1][j],f[i][j-1])
    return f[-1][-1]
               

N=int(input())
for i in range(N):
    x=input()
    y=input()
    print(lcs(x,y))

Python: from typing import List


def make_lcs(lhs, rhs):
    pass


class stub():
    def __init__(self):
        self.fin = open('task.txt')

    def stub_input(self):
        return self.fin.readline()


# _stub = stub()
# input = _stub.stub_input


def make_lcs(lhs: List[str], rhs: List[str]):
    c = [[0 for _ in range(len(rhs) + 1)] for _ in range(len(lhs) + 1)]
    for l_i in range(len(lhs)):
        for r_i in range(len(rhs)):
            if lhs[l_i] == rhs[r_i]:
                c[l_i + 1][r_i + 1] = c[l_i][r_i] + 1
            else:
                c[l_i + 1][r_i + 1] = max(c[l_i][r_i + 1], c[l_i + 1][r_i])
    print(c[-1][-1])


if __name__ == '__main__':
    n = int(input())
    for _ in range(n):
        lhs = [i for i in input().rstrip()]
        rhs = [i for i in input().rstrip()]
        make_lcs(lhs, rhs)

Python: def longest_common(x, y):
    cost = [0]
    for c in y:
        for i in range(len(cost) -1, -1, -1):
            index = x.find(c, cost[i]) + 1
            if index:
                if i + 1 < len(cost):
                    cost[i + 1] = min(cost[i + 1], index)
                else:
                    cost.append(index)
    return len(cost)  - 1
            

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

Python: import sys
def lcs(X, Y):
    c = [0 for _ in range(len(Y) + 1)]

    for i in X:
        tmp = c[:]
        for ji, yc in enumerate(Y):
            j = ji + 1
            if i == yc:
                c[j] = tmp[j - 1] + 1
            else:
                c[j] = max(tmp[j], c[j - 1])
    return c[y - 1]

n = int(input())

for i in range(n):
    X = sys.stdin.readline()[:-1]
    Y = sys.stdin.readline()[:-1]
    print(lcs(X, Y))

Python: def search(X, Y):
    dp = [[0] * (len(Y)+1) for x in range(len(X)+1)]
    for x in range(1, len(X) + 1):
        for y in range(1, len(Y) + 1):
            if X[x-1] == Y[y-1]:
                dp[x][y] = dp[x-1][y-1] + 1
            elif dp[x-1][y] > dp[x][y-1]:
                dp[x][y] = dp[x-1][y]
            else:
                dp[x][y] = dp[x][y-1]
    return dp[-1][-1]

N = int(input())
for i in range(N):
    X = input()
    Y = input()
    print(search(X, Y))

Python: n=int(input())
ans=[]
for k in range(n):
    s1=input()
    s2=input()
    dp=[[0 for i in range(len(s2)+1)] for j in range(len(s1)+1)]
    for i in range(1,len(s1)+1):
        for j in range(1,len(s2)+1):
            if s1[i-1]==s2[j-1]:
                dp[i][j]=dp[i-1][j-1]+1
            else:
                dp[i][j]=max(dp[i-1][j],dp[i][j-1])


    ans.append(dp[len(s1)][len(s2)])
for a in ans:
    print(a)

Python: def LCS(str1,str2):
    A = [0 for _ in range(len(str2)+1)]
    for i in range(len(str1)):
        A_copy = A[:]
        for j in range(len(str2)):
            if(str1[i]==str2[j]):
                A[j+1] = A_copy[j]+1
            elif(A[j] > A[j+1]):
                A[j+1] = A[j]
    print(A[-1])


m = int(input())
for i in range(m):
    str1 = input()
    str2 = input()
    LCS(str1,str2)

Python: import itertools

q = int(input())
for a in range(q):
    X = input()
    Y = input()
    cost = [0]
    for c in Y:
        for i in range(len(cost) - 1, -1, -1):
            index = X.find(c, cost[i]) + 1
            if index:
                if i + 1 < len(cost):
                    cost[i + 1] = min(cost[i + 1], index)
                else:
                    cost.append(index)
    print(len(cost) - 1)

Python: for _ in range(int(input())):
    X = input()
    Y = input()
    dp = [[0 for y in range(len(Y)+1)] for x in range(len(X)+1)]
    for x in range(1, len(X) + 1):
        for y in range(1, len(Y) + 1):
            if X[x-1] == Y[y-1]:
                dp[x][y] = dp[x-1][y-1] + 1
            else:
                dp[x][y] = max(dp[x-1][y], dp[x][y-1])
    print(dp[-1][-1])