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 <iostream>
#include <vector>

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;


int memo[1001][1001];
bool checked[1001][1001];
int lcs(string s, string t, int si, int ti) {
  
  if( si == -1 || ti == -1 ) {
    return 0;
  }
  if(checked[si][ti] == true) {
    return memo[si][ti];
  }
  checked[si][ti] = true;

  int res = 0;
  if( s[si] == t[ti] ) {
    res = lcs(s, t, si-1, ti-1) + 1;
  } else {
    res = max( lcs(s, t, si-1, ti), lcs(s, t, si, ti-1) );
  }

  return memo[si][ti] = res;

}

int solve() {

  string s, t;
  cin >> s >> t;

  // initialize memo 
  fill(checked[0], checked[1001], false);
  return lcs(s, t, s.size()-1, t.size()-1);


  // dp[i][j] := s_1,...s_i と t_1,...t_j に対するLCSの長さ
  // s[i] == t[j] なら dp[i-1][j-1] + 1
  // s[i] != t[j] なら max( dp[i-1][j], dp[i][j-1] )
  // int dp[s.size()+1][t.size()+1];
  // fill(dp[0], dp[s.size()+1], 0);
  // for(int i = 0; i < s.size(); i++) {
  //   for(int j = 0; j < t.size(); j++) {
  //     if( s[i] == t[j] ) {
  //       dp[i+1][j+1] = dp[i][j] + 1;
  //     } else {
  //       dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
  //     }
  //   }
  // }

  // return dp[s.size()][t.size()];



}


int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  for(int i = 0; i < N; i++) {
    cout << solve() << "\n";
  }


  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;
typedef long long int ll;
static const int N = 1000;

int lcs(string X, string Y) {
	int C[N + 1][N + 1];
	int M = X.size();
	int N = Y.size();
	int maxL = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (int i = 0; i <= M; i++) C[i][0] = 0;
	for (int i = 1; i <= N; i++) C[0][i] = 0;

	for (int i = 1; i <= M; i++) {
		for (int 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;
}

int main() {
	string S1, S2;
	int N; cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> S1 >> S2;
		cout << lcs(S1, S2) << endl;
	}
}

C++: #include <iostream>

using namespace std;
const int MAX = 1001;

short max(short a,short b) {
	if (a > b) {
		return a;
	} else {
		return b;
	}
}
short solve(string str1,string str2) {
	int len1 = str1.length();
	int len2 = str2.length();
	str1 = " " + str1;
	str2 = " " + str2;
	short LCS[MAX][MAX];
	for (int i=0;i<=len1;i++) {
		LCS[i][0] = 0;
	}
	for (int i=0;i<=len2;i++) {
		LCS[0][i] = 0;
	}
	int max1 = 0;

	for (int i=1;i<=len1;i++) {
		for (int j = 1;j<=len2;j++) {
			if (str1[i] == str2[j]) {
				LCS[i][j] = LCS[i-1][j-1] + 1;
			} else {
				LCS[i][j] = max(LCS[i-1][j],LCS[i][j-1]);
			}
			max1 = max(LCS[i][j],max1);
		}
	}
	return max1;
}

int main() {
	int n;
	cin >> n;
	for (int i=0;i<n;i++) {
		string str1,str2;
		cin >> str1 >> str2;
		cout << solve(str1,str2) << endl;
	}
	return 0;

}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;cin>>q;
  FOR(i,0,q){
    string x, y;
    cin >> x >> y;
    int n = x.length();
    int m = y.length();
    int dp[n+1][m+1];
    CLR(dp);
    FOR(i,0,n){
      FOR(j,0,m){
        if(x[i]==y[j]){
          dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
        }
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
      }
    }
    cout<<dp[n][m]<<endl;
  }
}

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

int main(){
  int n;
  std::string x,y;
  std::cin>>n;
  int a[1001][1001];
  int b[n];

  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1001;j++){
      for(int k=0;k<1001;k++){
	a[j][k]=0;
      }
    }
    for(int j=1;j<=x.size();j++){
      for(int k=1;k<=y.size();k++){
	if(x[j-1]==y[k-1]){
	  a[j][k]=a[j-1][k-1]+1;
	}else{
	  if(a[j-1][k]>a[j][k-1]){
	    a[j][k]=a[j-1][k];
	  }else{
	    a[j][k]=a[j][k-1];
	  }
	}
      }
    }
    b[i]=a[x.size()][y.size()];
  }

  for(int i=0;i<n;i++){
  std::cout<<b[i]<<"\n";
  }

  return 0;
}

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


int lcs(string a, string b) {
	int dp[1009][1009];

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

int main() {
	int q;
	cin >> q;
	getchar();
	while (q--) {
		string a, b;
		getline(cin, a);
		getline(cin, b);
		/*cout << "=======" << a << "=======" << endl;
		cout << "=======" << b << "=======" << endl;*/
		cout << lcs(a, b) << endl;
	}
	return 0;
}