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: import sys


def len_lcs(s1, s2):
    """Find length of the longest common sequence(LCS) of strings s1 and s2.
    The complexity is ~MN.

    >>> len_lcs("abc", "abc")
    3
    >>> len_lcs("abcbdaba", "bdcaba")
    5
    """
    results = [0] * len(s1)

    for i, c2 in enumerate(s2):
        sec1, pr1 = 0, 0
        for j, c1 in enumerate(s1):
            if c1 == c2:
                match = 1
            else:
                match = 0
            pr2 = results[j]
            mval = max(sec1, pr2, pr1 + match)
            results[j] = mval
            sec1, pr1 = mval, pr2

    return results[-1]


def run():
    n = int(input())
    s = []
    for line in sys.stdin:
        s.append(line.strip())
    for i in range(0, 2*n, 2):
        print(len_lcs(s[i], s[i+1]))


run()

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

Python: def length_of_lcs(x, y):
    n = len(y)
    #pre_row = [0] * (n + 1)
    cur_row = [0] * (n + 1)
    for x_c in x:
        prev_row = cur_row[:]
        for j, y_c in enumerate(y):
            if x_c == y_c:
                cur_row[j+1] = prev_row[j] + 1
            elif cur_row[j] > cur_row[j+1]:
                cur_row[j+1] = cur_row[j]

    return cur_row[n]


if __name__ == '__main__':
    N = int(input())
    for _ in range(N):
        x = input()
        y = input()
        print(length_of_lcs(x, y))

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

q = int(input())
for i in range(q):
    X = input()
    Y = input()
    print(lcs(X, Y))

Python: import sys
sys.setrecursionlimit(10000)

def calc(x,y):
    i = len(x)
    j = len(y)
    if not d[i][j] == None:
        return d[i][j]
    else:
        if x[-1] == y[-1]:
            tmp = max(calc(x[:-1],y[:-1])+1,calc(x[:-1],y),calc(x,y[:-1]))
            d[i][j] = tmp
            return tmp
        else:
            tmp = max(calc(x[:-1],y),calc(x,y[:-1]))
            d[i][j] = tmp
            return tmp
        
q = int(input())
for i in range(q):
    x = input()
    y = input()
    d = [[0 if i == 0 or j == 0 else None for i in range(len(y)+1)] for j in range(len(x)+1)]
    print(calc(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(n+1)] for _ in range(m+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
            else:
                c[i][j] = max(c[i-1][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: def longest_common_subsequence(X, Y):
    X = " " + X
    Y = " " + Y

    length_X = len(X)
    length_Y = len(Y)

    c = [0] * length_Y

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

        for j in range(1, length_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]


q = int(input())


for _ in range(q):

    X = input()
    Y = input()

    print(longest_common_subsequence(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: for _ in[0]*int(input()):
 X,Y=' '+input(),' '+input()
 m,n=len(X),len(Y)
 c=[[0]*n for _ in[0]*m]
 print(c)
 for i in range(1,m):
  for j in range(1,n):c[i][j]=(max(c[i-1][j],c[i][j-1]),1+c[i-1][j-1])[X[i]==Y[j]]
 print(c[m-1][n-1])

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(n+1)] for _ in range(m+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: 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
            elif dp[j+1] < dp[j]:
                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: def Ics(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)

    for i in range(a):
        d = x[i]
        c2 = c1[:]
        for j in range(n):
            if d == y[j]:
                c1[j + 1] = c2[j] + 1
            elif c1[j + 1] < c1[j]:
                c1[j + 1] = c1[j]
    return c1[-1]

q = int(input().rstrip())
r = []
for k in range(q):
    s1 = input().rstrip()
    s2 = input().rstrip()
    r.append(Ics(s1, s2))

print(*r, sep = "/n")

Python: n = int(input())
dataset = []
for i in range(n):
    x = input()
    y = input()
    dataset.append([x,y])

for data in dataset:
    m = len(data[0])
    n = len(data[1])
    dp = []
    for x in range(n+1):
        dp.append([0] *(m+1))

    for j in range(n):
        for i in range(m):
            if data[0][i] == data[1][j]:
                dp[j+1][i+1] = dp[j][i] + 1
            elif dp[j+1][i]>dp[j][i+1]:
                dp[j+1][i+1] = dp[j+1][i] 
            else:
                dp[j+1][i+1] = dp[j][i+1]

    print(dp[n][m])