Go: package main

/*
最長共通部分列
*/

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

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

Python: def lcs_len(X,Y):
    m,n=len(X),len(Y)
    LCS=[0 for _ in range(m+1)]
    for y in Y:
        previous=LCS[:]
        for i,x in enumerate(X):
            if x==y:
                LCS[i+1]=previous[i]+1
            elif LCS[i]>previous[i+1]:
                LCS[i+1]=LCS[i]
    return LCS[-1]

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

Python: m = int(input())
for i in range(m):
    str1 = input()
    str2 = input()
    A = [[0 for j in range(len(str1)+1)]for k in range(len(str2)+1)]
    for j in range(len(str2)):
        for k in range(len(str1)):
            if(str1[k]==str2[j]):
                A[j+1][k+1] = A[j][k]+1
            else:
                if(A[j+1][k] < A[j][k+1]):
                    A[j+1][k+1] = A[j][k+1]
                else:
                    A[j+1][k+1] = A[j+1][k]
    print(A[len(str2)][len(str1)])

Python: # coding: utf-8

def array2d(d1, d2, init = None):
    return [[init for _ in range(d2)] for _ in range(d1)]


def solve():
    x = input().strip()
    y = input().strip()

    # dp[i][j]: x[:i], y[:j] ???LCS
    # dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+1(if x[i]==y[j]))
    dp = array2d(len(x), len(y), 0)
    dp[0][0] = int(x[0] == y[0])
    for j in range(1, len(y)):
        dp[0][j] = max(dp[0][j-1], int(x[0] == y[j]))
    for i in range(1, len(x)):
        dp[i][0] = max(dp[i-1][0], int(x[i] == y[0]))
        for j in range(1, len(y)):
            dp[i][j] = max(dp[i-1][j], dp[i][j-1])
            if x[i] == y[j]:
                dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1)
    return dp[-1][-1]


def main():
    cases = int(input())
    for i in range(cases):
        print(solve())

main()

Python: query = int(input())
for _ in range(query):
    A = input().strip()
    B = input().strip()
    dp = [[0 for n in range(len(A) + 1)] for m in range(len(B) + 1)]
    for i in range(len(B) + 1):
        for inner in range(len(A) + 1):
            if i == 0:
                continue
            elif inner == 0:
                choice = dp[i - 1][inner]
            else:
                choice = max(dp[i][inner - 1], dp[i - 1][inner], dp[i - 1][inner - 1] + int(A[inner - 1] == B[i - 1]))
            dp[i][inner] = choice
#                 print(i, inner, A[inner - 1], B[i - 1])
#         print(dp)
    print(dp[len(B)][len(A)])

Python: import sys


def lcs(x, y):
    # Creating len(x + 1) * len(y + 1) table
    x = ' ' + x  # Add Space
    y = ' ' + y  # Add Space
    len_x = len(x)
    len_y = len(y)
    table = []
    for _ in range(len_y):
        table.append([0] * len_x)

    maxl = 0
    for i, x_ch in enumerate(x):
        if i == 0: continue
        for j, y_ch in enumerate(y):
            if j == 0: continue
            if x[i] == y[j]:
                table[j][i] = table[j - 1][i - 1] + 1
            else:
                table[j][i] = max(table[j - 1][i], table[j][i - 1])
            maxl = max(maxl, table[j][i])

    return maxl


if __name__ == "__main__":
    lines = [line.strip() for line in sys.stdin.readlines()[1:]]
    for sequence_pair in zip(lines[::2], lines[1::2]):
        print(lcs(*sequence_pair))

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

最長共通部分列
"""


c = []
def prep_array(X, Y):
    global c
    """ 値が0の2次元配列を用意する。サイズは入力データの最大値を想定して縦横(1000+1)以上必要。 """
    # from array import array
    # c = [array('I', [0] * (len(X)+1)) for _ in range(len(Y)+1)]  #  arrayで用意
    c = [[0]*(len(Y)+2) for _ in range(len(X)+2)] #  リストで用意
    return c


def check_max(array):
    """ 2次元配列の中の最大の値を返す """
    max_lcs = 0
    for row in array:
        temp_max = max(row)
        if temp_max > max_lcs:
            max_lcs = temp_max
    return max_lcs


def calc_lcs4(X, Y, c):
    """
    Y[j]のアクセス方法を変更
    リストの値を一旦変数に保存するように変更
    最長共通部分長を関数内で計算するように変更
    4/5 のテストデータを使用してプロファイリングすると実効時間は 5.6秒
    """
    X = ' ' + X
    Y = ' ' + Y
    m = len(X)
    n = len(Y)
    max_lcs = 0

    pre_row = c[0]
    for i in range(1, m):
        row = c[i]
        XX = X[i]
        for j, YY in enumerate(Y):
            if XX == YY:
                pr_j1 = pre_row[j - 1]
                row[j] = pr_j1 + 1
                if pr_j1+1 > max_lcs:
                   max_lcs = pr_j1 + 1
            else:
                pr_j = pre_row[j]
                r_j1 = row[j-1]
                if pr_j >= r_j1:
                    row[j] = pr_j
                else:
                    row[j] = r_j1
        pre_row = row
    return max_lcs


if __name__ == '__main__':
    # データの入力
    num = int(input())
    #f = open('input.txt')
    #num = int(f.readline())

    for _ in range(num):
        X = input().strip()
        Y = input().strip()

        # 処理の実行
        result = calc_lcs4(X, Y, prep_array(X, Y))

        # 結果の表示
        print(result)

Python: N = int(input())

for _ in range(N):
    x = input()
    y = input()
    m, n = len(x), len(y)
    dp = [0] * (n+1)
    
    for xi in x:
        temp = dp[:]
        for j in range(n):
            if xi == y[j]:
                dp[j+1] = temp[j] + 1
            elif dp[j+1] < dp[j]:
                dp[j+1] = dp[j]
                
    print(dp[-1])