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: from collections import defaultdict
from bisect import bisect_left, bisect, insort_left, insort
INF = 10000
def lcs(s1, s2):
    c = defaultdict(list)
    for i, s in enumerate(s1):
        c[s].append(i)
    for s in set(s2):
        c[s].append(INF)
    def g(li, a):
        for i in li:
            if i >= a:
                return i
        return -1
    dp = []
    for s2_k in s2:
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):
            chr_idx = c[s2_k][bisect_left(c[s2_k], bgn_idx)]
            if chr_idx == INF:
                continue
            dp[i] = min(cur_idx, chr_idx + 1)
            bgn_idx = cur_idx
        else:
            chr_idx = c[s2_k][bisect_left(c[s2_k], bgn_idx)]
            if chr_idx != INF:
                dp.append(chr_idx + 1)
    return len(dp)
def solve():
    N = int(input())
    for _ in range(N):
        x = input()
        y = input()
        print(lcs(x, y))
solve()

Python: # -*- coding: utf-8 -*-

import random
import sys
import os
import pprint


def LCS(s0, s1):
    # print(s0)
    # print(s1)
    m = len(s0)
    n = len(s1)

    # memo[i][j]
    memo = [[0] * n for i in range(m)]
    # pprint.pprint(memo)

    # fill at index 0
    for i in range(m):
        if s1[0] in s0[0:i+1]:
            memo[i][0] = 1
    for j in range(n):
        if s0[0] in s1[0:j+1]:
            memo[0][j] = 1

    # fill memo
    for i in range(1, m):
        for j in range(1, n):
            # same last
            if s0[i] == s1[j]:
                memo[i][j] = memo[i-1][j-1] + 1
            else:
                memo[i][j] = max(memo[i-1][j], memo[i][j-1])
    return memo[m-1][n-1]


def LCS_fast(x, y):
    m = len(x)
    n = len(y)
    cost = [0] * n
    for i in range(m):
        x_char = x[i]
        tmp = cost[:]
        for j in range(n):
            if x_char == y[j]:
                cost[j] = tmp[j] + 1
            elif j == 0:
                cost[j] = 0
            elif cost[j] < cost[j-1]:
                cost[j] = cost[j-1]
    return cost[n-1]

#fd = os.open('ALDS1_10_C.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

n = int(input())
for _ in range(n):
    s0 = input()
    s1 = input()
    #l = LCS(s0, s1)
    l2 = LCS_fast(s0, s1)
    #print(l)
    print(l2)

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