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 <algorithm>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <memory.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)


int main(int argc, const char * argv[]) {
    int Q;
    string X, Y;
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        cin >> X >> Y;
        int DP[1001][1001];
        memset(DP, 0, sizeof(DP));
        for (int sx = 1; sx <= X.size(); ++sx) {
            for (int sy = 1; sy <= Y.size(); ++sy) {
                DP[sx][sy] = max(DP[sx-1][sy], DP[sx][sy-1]);
                if (X[sx] == Y[sy]) {
                    DP[sx][sy] = max(DP[sx][sy], DP[sx-1][sy-1]+1);
                }
            }
        }
        cout << DP[X.size()][Y.size()] << endl;
    }
    return 0;
}

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

int q;
int len[1005][1005];

int common(string a,string b,int m,int n){
    int i,j;
    for(i=0;i<=m;i++)len[i][0]=0;
    for(i=0;i<=n;i++)len[0][i]=0;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            if(a[i]==b[j])len[i+1][j+1]=len[i][j]+1;
            else len[i+1][j+1]=max(len[i+1][j],len[i][j+1]);
        }
    }
    return len[m][n];
}

int main()
{
    cin>>q;
    int i;
    for(i=0;i<q;i++){
        string a,b;
        cin>>a>>b;
        int m=a.length();
        int n=b.length();
        cout<<common(a,b,m,n)<<endl;
    }
    return 0;
}

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

int dp[1001][1001];
string str1, str2;
int n;

int solve(string str1, string str2) {
  memset(dp, 0, sizeof(dp));
  for(int r = 0; r < str2.size(); r++) {
    for(int c = 0; c < str1.size(); c++) {
      if(str2[r] == str1[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[str2.size()][str1.size()];
}

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

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

using namespace std;

string str1,str2;

int m[1000][1000];

int LCS(int i,int j){
  
  
  if(i==-1||j==-1){
    return 0;
  }else if(m[i][j]!=0){
    return m[i][j];
  }else if(str1[i]==str2[j]){
    m[i][j]=LCS(i-1,j-1)+1;
    return m[i][j];
  }else{
    m[i][j]=max(LCS(i-1,j),LCS(i,j-1));
    return m[i][j];
  }
  
}

int main(){
  int N;
  
  cin >> N;
  
  
  for(int i=0;i<N;i++){
    cin >> str1 >> str2;
    for(int j=0;j<1000;j++){
      for(int k=0;k<1000;k++){
        m[j][k]=0;
      }
    }
    cout << LCS(str1.size()-1,str2.size()-1) << endl;
  }
  return 0;
}

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

int main(){
  int Q; cin >> Q;
  for(int i = 0; i < Q; i++){
    string s, t; cin >> s >> t;
    int N = s.size(), M = t.size();
    vector<vector<int> > dp(N + 1, vector<int> (M + 1, 0));
    for(int i = 0; i < N; i++){
      for(int j = 0; j < M; 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]);
        }
      }
    }
    cout << dp[N][M] << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int dp[1010][1010];
int main(){
    //cout<<"Yes"<<endl;
    int n;
    cin>>n;
    rep(k,n){
        //cout<<"aaa"<<endl;
        string s,t;
        cin>>s>>t;
        rep(i,1010) rep(j,1010) dp[i][j]=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+1][j],dp[i][j+1]);
                }
            }
        }
        cout<<dp[s.size()][t.size()]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
using namespace std;
int dp[1000][1000];
string s1,s2;
int len1,len2;

int Str(){
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=len1;i++)
		for(int j=1;j<=len2;j++){
			if(s1[i]==s2[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[len1][len2];
	}
	
	int main(){
		int n;
		cin>>n;
		while(n--){
			cin>>s1>>s2;
			len1=s1.size();
			len2=s2.size();
			s1=' '+s1;
			s2=' '+s2;
			cout<<Str()<< '\n';
		}
		return 0;
	}

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

int lcs(string a,string b){
    int la=a.size();
    int lb=b.size();

    int array[la+1][lb+1];
    for(int i=0;i<=la;i++){
        array[i][0]=0;
    }
    for(int i=0;i<=lb;i++){
        array[0][i]=0;
    }
    for(int i=1;i<=lb;i++){
        for(int j=1;j<=la;j++){
            if(a[j-1]==b[i-1]){
                array[j][i]=array[j-1][i-1]+1;
            }
            else{
                if(array[j-1][i]>array[j][i-1]){
                    array[j][i]=array[j-1][i];
                }
                else{
                    array[j][i]=array[j][i-1];
                }
            }
        }
    }
    return array[la][lb];
}

int main(){
    string a,b;
    int d;
    cin>>d;
    int c[d];
    for(int i=0;i<d;i++){
        cin>>a>>b;
        c[i]=lcs(a,b);  
    }
    for(int i=0;i<d;i++){
        cout<<c[i]<<endl;
    }
    return 0;
}