Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// if S[i-1] == T[j-1] {
				// 	dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				// } else {
				// 	dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				// }
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(v) (v).begin(), (v).end()
#ifdef DBGPRT
#define dbg(n) cerr << "//" << #n << ":" << n <<endl
#else
#define dbg(n) ;
#endif
typedef pair<ll, ll> LLARR;
#define INF 999999999
const double PI = 3.1415926535897932384626433832795;

ll lcs(string s1, string s2)
{
	int m = len(s1);
	int n = len(s2);
	s1 = ' '+s1;
	s2 = ' '+s2;
	int (*mat)[1001];
	mat = new int[1001][1001];
	REP(i, m)
	{
		mat[i][0] = 0;
	}
	REP(i, n)
	{
		mat[0][i] = 0;
	}
	int maxx = 0;
	FOR(i, 1, m+1)
	{
		FOR(j, 1, n+1)
		{
			if (s1[i] == s2[j])
				mat[i][j] = mat[i-1][j-1] + 1;
			else
				mat[i][j] = max(mat[i][j-1], mat[i-1][j]);
			maxx = max(maxx, mat[i][j]);
		}
	}
	delete[] mat;
	return maxx;
}
int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	REP(i, n)
	{
		string s1;
		cin >> s1;
		string s2;
		cin >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

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

int main(){
    int Q;
    string str_x, str_y;
    int memo[1001][1001];
    cin >> Q;
    
    for(int q = 0;q < Q;q++){
        cin >> str_x >> str_y;
        
        for(unsigned int i = 0;i < str_x.size();i++){
            for(unsigned int j = 0;j < str_y.size();j++){
                if(str_x.at(i) == str_y.at(j)){
                    memo[i+1][j+1] = memo[i][j] + 1;
                }else{
                    memo[i+1][j+1] = max(memo[i][j+1], memo[i+1][j]);
                }
            }
        }
        
        printf("%d\n", memo[str_x.size()][str_y.size()]);
    }
    
    return 0;
}

C++: #include <iostream>

int lcs(std::string x, std::string y)
{
  int m = x.length();
  int n = y.length();
  int cs[m + 1][n + 1];

  for (int i = 0; i < m + 1; ++i)
    cs[i][0] = 0;
  for (int j = 1; j < n + 1; ++j)
    cs[0][j] = 0;

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

  return cs[m][n];
}

int main()
{
  int q;
  std::cin >> q;

  std::string x, y;
  for (int i = 0; i < q; ++i)
  {
    std::cin >> x >> y;
    std::cout << lcs(x, y) << std::endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stdio.h>
#include <math.h>


using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;

typedef pair<int, int> P;

typedef queue<int> qi;
typedef queue<string> qs;


#define rep(i, n) for(int i=0; i<(n); ++i)
#define repp(i, a, b) for(int i = (a); i <= (b); i++)
#define Rep(i, n) for(int i=n; i >= 0; i--)

#define all(v) v.begin(), v.end()
//sort( all(v) )　などと使える

const int INF = 50010;

int main() {
	int q; cin >> q;
	rep(i, q) {
		string x, y; cin >> x >> y;
		int n = x.size(), m = y.size();

		vvi dp(n + 1, vi(m + 1, 0));

		rep(i, n) rep(j, m) {
			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[n][m] << 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();
	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 <cstdio>
#include <cstring>
#include <algorithm>

class TLcsSolver
{
    int FTable[1001][1001];
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
    void Print()const
    {
        for(std::size_t i = 0; i < Fn1; ++i){
            for(std::size_t j = 0; j < Fn2; ++j){
                printf("%d\t", FTable[i][j]);
            }
            printf("\n");
        }
    } 
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[Fn1-1][Fn2-1];
    }
};

int main()
{
    int n;
    std::scanf("%d\n", &n);
    char a[1001];
    char b[1001];
    for(int i = 0; i < n; ++i){
        std::fgets(a, 1001, stdin);
        std::fgets(b, 1001, stdin);
        std::printf("%d\n", TLcsSolver(a, b).Answer());
    }
    return 0;
}