Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for j := 1; j <= n; 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])
			}
			maxl = max(maxl, c[i][j])
		}
	}
	return maxl
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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, 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, 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: from itertools import product
q = int(input())

def solve(X, Y):
    LCS = [[0]*(len(Y)+1) for _ in range(len(X)+1)]
    for x, y in product(range(1, len(X)+1), range(1, len(Y)+1)):
        if X[x-1] == Y[y-1]:
            LCS[x][y] = LCS[x-1][y-1] + 1
        else:
            LCS[x][y] = max(LCS[x-1][y], LCS[x][y-1])
    return(LCS[-1][-1])

for _ in range(q):
    X = input()
    Y = input()
    print(solve(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(1,len_a+1):
        for j in range(1,len_b+1):
            if a[i-1] == b[j-1]:
                table[i][j] = table[i-1][j-1] + 1
            else:
                table[i][j] = max(table[i-1][j],table[i][j-1])
    print(table[len_a][len_b])

Python: import sys
readline = sys.stdin.readline
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)
    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:
                break
            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 = readline().strip()
        y = readline().strip()
        print(lcs(x, y) if len(x) < len(y) else lcs(y, x))
solve()

Python: def lcs(str1,str2):
    c=[[0 for _ in range(100)] for _ in range(100)]
    m=len(str1)
    n=len(str2)
    maxl=0
    str1=" "+str1
    str2=" "+str2

    i=1
    j=1
    while(i<=m):
        while(j<=n):
            if(str1[i]==str2[j]):
                c[i][j]=c[i-1][j-1]+1
              #  print(c[i][j])
            else:
                c[i][j]=max(c[i-1][j],c[i][j-1])
            maxl=max(maxl,c[i][j])
            j+=1
        i+=1
        j=0

    return maxl

def main():
    n=int(input())
    i=0
    while(i<n):
        str1=str(input())
        str2=str(input())
        print(int(lcs(str1,str2)))
        i+=1

main()

Python: def len_lcs(s1, s2):
    """Find length of the longest common sequence(LCS) of strings s1 and s2.

    >>> len_lcs("abc", "abc")
    3
    >>> len_lcs("abcbdaba", "bdcaba")
    5
    """
    def match(c1, c2):
        if c1 == c2:
            return 1
        else:
            return 0

    if len(s1) == 0 or len(s2) == 0:
        return 0

    primary = [0] * len(s1)
    secondary = [0] * len(s1)

    for i in range(len(s2)):
        for j in range(len(s1)):
            if j == 0:
                secondary[j] = max(primary[j], match(s1[j], s2[i]))
            else:
                secondary[j] = max(secondary[j-1], primary[j],
                                   primary[j-1] + match(s1[j], s2[i]))

        primary, secondary = secondary, primary

    return primary[len(s1)-1]


def run():
    n = int(input())

    for _ in range(n):
        s1 = input()
        s2 = input()
        print(len_lcs(s1, s2))


if __name__ == '__main__':
    run()

Python: import sys
q = int(sys.stdin.readline().strip())
for _ in range(q):
    x = sys.stdin.readline().strip()
    y = sys.stdin.readline().strip()
    n = len(x)
    m = len(y)
    dp = [0 for j in range(m + 1)]

    for i in range(1, n + 1):
        dp_prev = dp[:]
        for j in range(1, m + 1):
            if x[i - 1] == y[j - 1]:
                dp[j] = dp_prev[j - 1] + 1
            elif dp_prev[j] < dp[j - 1]:
                dp[j] = dp[j - 1]
                
    print(dp[m])

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


n = int(input())

def calc(data1, data2):
    term = len(data1)+1
    table = [0]*term

    for i, d2 in enumerate(data2):
        tmp_table = table[:]
        for j, d1 in enumerate(data1):
            if d1 == d2:
                table[j + 1] = tmp_table[j] + 1
            elif table[j+1] < table[j]:
                table[j + 1] = table[j]

    print(table[term-1])
    return 

for _ in range(n):
    data1 = sys.stdin.readline().rstrip()
    data2 = sys.stdin.readline().rstrip()
    calc(data1, data2)

Python: def lcs_fast(x, y):
    len_x = len(x)
    len_y = len(y)

    c = [0] * (len_y + 1)
    for i in range(len_x):
        c_prev = c[:]
        for j in range(len_y):
            if x[i] == y[j]:
                c[j+1] = c_prev[j] + 1
            elif c[j+1] < c[j]:
                c[j+1] = c[j]
    return c[-1]


if __name__ == "__main__":
    n = int(input())
    for i in range(n):
        x = input()
        y = input()
        # print(lcs(x, y))
        print(lcs_fast(x, y))

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <stack>
#include <queue>
#include <list>
#include <stdio.h>
#include <algorithm>
#define MAX 100000
using namespace std;
typedef long long ll;
int dp[1002][1002];
int lcs(string a,string b)
{
	a=" "+a;
	b=" "+b;
	int m,n;
	m=a.size();
	n=b.size();
	
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			if(a[i]==b[j]){
				dp[i][j]=dp[i-1][j-1]+1;
			}else{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	return dp[m][n]-1;
}
int main()
{	
	int n;
	string a,b;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		cout<<lcs(a,b)<<endl;
	}
	return 0;
}