Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i][j-1], dp[i-1][j])
			}
		}
	}
	return dp[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

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