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] = 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++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    int Q;
    cin >> Q;
    while (Q--) {
        string X, Y;
        cin >> X >> Y;
        ll N = X.size();
        ll M = Y.size();
        vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, 0));
        rep(i, N) {
            rep(j, 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]});
            }
        }
        cout << dp[N][M] << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

C++: 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char len1[1001], len2[1001];
 
LCS(int x, int y){
  int i,j,c[1001][1001];
 
   for(i=0;i<=x;i++){
      c[i][0]=0;
  }
 
  for(j=0;j<=y;j++){
    c[0][j]=0;
  }
 
  for(i=1;i<=x;i++){
    for(j=1;j<=y;j++){
      if(len1[i-1]==len2[j-1]){
    c[i][j]=c[i-1][j-1]+1;
      }else if(c[i-1][j-1]>=c[i][j-1]){
    c[i][j] = c[i-1][j];
      }else{
    c[i][j] = c[i][j-1];
      }
    }
  }
 
  if(i==0 || j==0){
    return 0;
  }else if(i>0 && j>0 && x == y){
    return c[i-1][j-1];
  }else if(i>0 && j>0 && x!=y){
    return c[x][y];
  }
}
 
main(){
  int n,i,l1,l2,result;
 
  scanf("%d", &n);
   
  for(i=0;i<n;i++){
    scanf("%s",len1);
    scanf("%s",len2);
    l1=strlen(len1);
    l2=strlen(len2);
    
    result=LCS(l1,l2);
    printf("%d\n",result);
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
static const int N = 1000;

int lcs(string x, string y);

int main() {
    int n;
    cin >> n;
    string x, y;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }
    return 0;
}
int lcs(string x, string y) {
    int c[N + 1][N + 1];
    int m = (int)x.size(), n = (int)y.size();
    for (int i = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 1; j <= n; j++) c[0][j] = 0; //c[0][0] is prepared above
    int maxl = 0;
    x = ' ' + x;
    y = ' ' + y;
    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]);
        }
    }
    /*for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            cout << c[i][j] << " ";
        }
        cout << endl;
    }*/
    return maxl;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N = 1000;

int lcs(string X,string Y){
int c[N+1][N+1];
int m = X.size();
int n = X.size();
int maxl = 0;
X = ' '+X;
Y = ' '+Y;
for(int i = 1;i<=m;i++)c[i][0] = 0;
for(int j = 1;j<=n;j++)c[0][j] = 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;
    }
return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
    int q;
    string x,y;
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>x>>y;
        int m=x.size();
        int n=y.size();
        int c[m+1][n+1];
        for(int j=0;j<=m;j++){
            c[j][0]=0;
        }
        for(int j=1;j<=n;j++){
            c[0][j]=0;
        }
        for(int j=1;j<=m;j++){
            for(int k=1;k<=n;k++){
                if(x[j-1]==y[k-1]){
                    c[j][k]=c[j-1][k-1]+1;
                }
                else{
                    c[j][k]=max(c[j-1][k],c[j][k-1]);
                }
            }
        }
        cout<<c[m][n]<<endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

int dp[1010][1010];
int main(){
	ios::sync_with_stdio(false);
	int t;
	string a,b;
	cin>>t;
	while(t--){
		memset(dp,0,sizeof(dp));
		cin>>a>>b;
		int lena=a.length();
		int lenb=b.length();
		for(int i=1;i<=lena;i++){
			for(int j=1;j<=lenb;j++){
				if(a[i-1]==b[j-1]){
					dp[i][j]=dp[i-1][j-1]+1;
				}
				else{
					dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
				}
			}
		}
		cout<<dp[lena][lenb]<<endl;
	}


	return 0;
}