Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<n;++i)

const int N = 1000;

int main() {
  int q, lmax;
  int c[N + 1][N + 1] = {0};
  string X, Y;

  cin >> q;
  REP(i, q) {
    cin >> X >> Y;

    X = ' ' + X;
    Y = ' ' + Y;
    lmax = 0;
    REP(i, X.length()) REP(j, Y.length()) {
      if (i == 0 || j == 0) continue;
      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]);
      }
      lmax = max(lmax, c[i][j]);
    }

    cout << lmax << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	int n;
	cin >> n;

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		string a, b;
		cin >> a >> b;

		for (int i = 1; i <= a.size(); i++)
		{
			for (int j = 1; j <= b.size(); j++)
			{
				if (a[i - 1] == b[j - 1])
					memo2[j] = max({ memo2[j - 1], memo1[j], memo1[j - 1] + 1 });
				else
					memo2[j] = max(memo2[j - 1], memo1[j]);
			}
			swap(memo1, memo2);
			memo2[0] = 0;
		}

		cout << memo1[b.size()] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    int q;
    cin >> q;
    while (q--) {
        string X, Y;
        cin >> X >> Y;
        int dp[X.size() + 1][Y.size() + 1];
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < X.size(); ++i) {
            for (int j = 0; j < Y.size(); ++j) {
                if (X[i] == Y[j]) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                } else {
                    dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
                }
            }
        }
        cout << dp[X.size()][Y.size()] << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

#define NUM 1000

string X;
string Y;
int dp[NUM + 1][NUM + 1];

void init(){
    for(int i = 0; i < NUM + 1; i++){
        for(int j = 0; j < NUM + 1; j++){
            dp[i][j] = 0;
        }
    }
}

void calc(void){
    init();

    for(int j = 1; j <= Y.size(); j++){
        for(int i = 1; i <= X.size(); i++){
            if(Y[j] == X[i]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }else{
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    
    printf("%d\n", dp[X.size()][Y.size()]);

}

int main(void){
    int q;
    scanf("%d", &q);

    for(int i = 0; i < q; i++){
        cin >> X;
        cin >> Y;
        calc();
    }
}

C++: #include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int solve(std::string x, std::string y) {
  int m = x.length();
  int n = y.length();
  int lcs[m + 1][n + 1];
  rep(i, m + 1) lcs[i][0] = 0;
  rep(j, n + 1) lcs[0][j] = 0;

  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (x[i - 1] == y[j - 1])
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      else
        lcs[i][j] = std::max(lcs[i - 1][j], lcs[i][j - 1]);
    }
  }

  return lcs[m][n];
}

int main() {
  int n;
  std::cin >> n;
  std::string x, y;

  rep(i, n) {
    std::cin >> x >> y;
    std::cout << solve(x, y) << '\n';
  }
  return 0;
}

C++: q = int(input())
ans = []
for i in range(q):
    x = input()
    y = input()
    n = len(x)
    m = len(y)
    dp1 = [0]*(m+1)
    for i in range(n):
        s = x[i]
        dp2 = dp1[:]
        for j in range(m):
            if s == y[j]:
                dp1[j+1] = dp2[j]+1
            elif dp1[j+1] < dp1[j]:
                dp1[j+1] = dp1[j]
    ans.append(dp1[-1])
print(*ans, sep="\n")

C++: #include <iostream>
#include <string>

using namespace std;

//L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS
int L[1001][1001]={};
int main(){
  int testnum;
  cin>>testnum;
  for(int t=0;t<testnum;++t){
    string X,Y;
    cin>>X>>Y;

    for(int i=1;i<=X.length();++i){
      for(int j=1;j<=Y.length();++j){
        if(X[i]==Y[j])
          L[i][j] = 1 + L[i-1][j-1];
        else
          L[i][j] = max(L[i][j-1],L[i-1][j]);
      }
    }

    cout << L[X.length()][Y.length()] << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll t1,t2,t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main(){
  ll q;
  cin >> q;
  string str1,str2;
  rep(_,q){
    cin >> str1;
    cin >> str2;
    ull len1 = str1.size();
    ull len2 = str2.size();
    vector<vector<ll>> dp(len1+1,vector<ll> (len2+1,0));
    rep(i,len1){
      rep(j,len2){
        dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
        if(str1[i] == str2[j]){
          chmax(dp[i+1][j+1],dp[i][j] + 1);
        }
      }
    }
    cout << dp[len1][len2] << endl;
  }
}