Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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)



def solve():

    N = int(input())

    for _ in range(N):

        x = input()

        y = input()

        print(lcs(x, y))



if __name__ == '__main__':

    solve()

Python: # 38

import sys


def lcs(X, Y):
    l_x = len(X)
    l_y = len(Y)
    # dp[i][j] : Xのi文字目、Yのj文字目までから構成される最長部分列数
    dp = [[0] * (l_y+1) for _ in range(l_x+1)]
    for i in range(l_x):
        for j in range(l_y):
            if X[i] == Y[j]:
                dp[i+1][j+1] = dp[i][j]+1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
    return dp[-1][-1]


def lcs_s(X, Y):
    L = []
    for yk in Y:
        bgn_idx = 0
        for i, cur_idx in enumerate(L):
            chr_idx = X.find(yk, bgn_idx) + 1
            if not chr_idx:
                break
            L[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        else:
            chr_idx = X.find(yk, bgn_idx) + 1
            if chr_idx:
                L.append(chr_idx)
    return len(L)


Q = int(input())
for _ in range(Q):
    X = sys.stdin.readline().rstrip()
    Y = sys.stdin.readline().rstrip()
    print(lcs_s(X, Y))

Python: import sys

def dp_init(m, n):
    return [[0] * n] * m

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)

def rec(i, j, str_a, str_b, dp):
    if i-1 == 0 or j-1 == 0:
        return 0
    if dp[i-1][j-1] > 0:
        return dp[i-1][j-1]
    if str_a[i-1] == str_b[j-1]:
        dp[i-1][j-1] = rec(i-1, j-1, str_a, str_b, dp) + 1
        print("(%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), rec(i-1, j, str_a, str_b, dp))
    print("(%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: # -*- 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: def gen_dp(row, col):
  return [[0]*col for i in range(row)]

def solver(str_1 ,str_2):
  dp = gen_dp(len(str_2) + 1, len(str_1) + 1)
  for r in range(len(str_2)):
    for c in range(len(str_1)):
      if str_2[r] == str_1[c]:
        dp[r + 1][c + 1] = dp[r][c] + 1
      else:
        dp[r + 1][c + 1] = max(dp[r + 1][c], dp[r][c + 1])
  return dp[len(str_2)][len(str_1)]

n = int(input())
for _ in range(n):
  str_1 = input()
  str_2 = input()
  print(solver(str_1, str_2))

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

Python: import sys


def lcs(X, Y):
    m = len(X)
    n = len(Y)
    c = [[0] * (n+1) if i == 0 else [0] + [None] * n for i 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
            elif c[i-1][j] >= c[i][j-1]:
                c[i][j] = c[i-1][j]
            else:
                c[i][j] = c[i][j-1]
    return c[m][n]


if __name__ == "__main__":
    n = int(sys.stdin.readline())
    inp = sys.stdin.readlines()
    for i in range(n):
        print(lcs(inp[2*i][:-1], inp[2*i+1][:-1]))