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: def solveZ(X, Y, Z, x, y):
	if x < len(X) and y < len(Y):
		ans = [solveZ(X, Y, Z, x, y+1)]
		Z.append(Y[y])
		for j in range(x, len(X)):
			if Y[y] == X[j]:
				ans.append(solveZ(X, Y, Z, j+1, y+1))
		return max(ans)
	else:
		return len(Z)

q = int(input())
for i in range(q):
	X = input()
	Y = input()
	ans[i] = solveZ(X, Y, [], 0, 0)

for i in range(q):
	print(ans[i])

Python: def main():
    n = int(input())
    for i in range(n):
        a = input()
        b = input()
        na=len(a)
        nb=len(b)
        array = [[0]*nb for i in range(na)]
        #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 i==0:
                    if a[i]==b[j]:
                        array[i][j]=1
                elif j==0:
                    if a[i]==b[j]:
                        array[i][j]=1
                else:
                    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])
if __name__ == "__main__":
    main()

Python: def lcs(X,Y):
  hist = [0]

  for y in Y:
    for i in range(len(hist)-1, -1, -1):
      found = X.find(y, hist[i])+1
      if found:
        if i+1 >= len(hist):
          hist.append(found)
        else:
          hist[i+1] = min(hist[i+1], found)
  return len(hist)-1


n = int(input())
for _ in range(n):
  a = input()
  b = input()
  print(lcs(a,b))

Python: from collections import defaultdict

def longest_common_subsequence(X, Y):
    len_X = len(X)
    len_Y = len(Y)
    c = [0] * len_Y

    for i in range(1, len_X):
        c_ = c[:]

        for j in range(1, len_Y):

            if X[i] == Y[j]:
                c[j] = c_[j - 1] + 1
            elif c_[j] < c[j - 1]:
                c[j] = c[j - 1]
    
    return c[-1]

def Main():
    Q = int(input())

    for q in range(Q):
        X = " " + input()
        Y = " " + input()
        print(longest_common_subsequence(X, Y))

Main()

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


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

Python: import numpy as np
import time
def f(i,j):
    if i == 0 or j == 0:
        if s1[i] == s2[j]:
            x[i][j] = 1
        else:
            if i == 0:
                x[i][j] = x[i][j-1]
            else:
                x[i][j] = x[i-1][j]
    elif s1[i] == s2[j]:
        x[i][j] = max(x[i-1][j-1]+1,x[i][j-1],x[i-1][j])
    else:
        x[i][j] = max(x[i-1][j-1],x[i][j-1],x[i-1][j])

t1 = time.time()
n = int(input())
for i in range(n):
    s1 = input()
    s2 = input()
    x = np.zeros((len(s1),len(s2)))
    for j in range(len(s1)):
        for k in range(len(s2)):
            f(j,k)
            
    print(int(x[len(s1)-1][len(s2)-1]))
t2 = time.time()

Python: for _ in[0]*int(input()):
 X,z=input(),[]
 for y in input():
  s=i=0
  for k in z:
   t=X.find(y,s)+1
   if t<1:break
   if t<k:z[i]=t
   s=k;i+=1
  else:
   t=X.find(y,s)+1
   if t:z+=[t]
 print(len(z))

Python: import sys
sys.setrecursionlimit(10000)
def lcs(X, Y):
    i = len(X)
    j = len(Y)
    if not i or not j:
        return 0
    if memo[i][j] >= 0:
        return memo[i][j]
    if X[-1] == Y[-1]:
        return 1 + lcs(X[:-1], Y[:-1])
    else:
        memo[i][j] = max(lcs(X[:-1], Y), lcs(X, Y[:-1]))
        return memo[i][j]

q = int(input())
for i in range(q):
    X = input()
    Y = input()
    memo = [[-1 for j in range(len(Y) + 1)] for i in range(len(X) + 1)]
    print(lcs(X, Y))

Python: n = int(input())

for k in range(n):
    a = input()
    b = input()
    len_a, len_b = len(a), len(b)
    table = [[0] * (len_b +1) for i in range(len_a + 1)]
    
    for i in range(len_a):
        for j in range(len_b):
            if a[i] == b[j]:
                table[i+1][j+1] = table[i][j] + 1
            else:
                table[i+1][j+1] = max(table[i][j+1],table[i+1][j])
    print(table[len_a][len_b])

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

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

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

Python: # -*- coding: utf-8 -*-
"""
Created on Fri May  4 20:33:37 2018
ALDS1_10_C
@author: maezawa
"""

def longest(x, y):
    m = len(x)
    n = len(y)
    c = [[0 for _ in range(m+1)] for _ in range(n+1)]
    for i in range(m):
        for j in range(n):
            print(i,j)
            if x[i] == y[j]:
                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-1][n-1]
                
    

q = int(input())

for i in range(q):
    x = input()
    y = input()
    
    print(longest(x, y))

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