Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for 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
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int MAX_LEN = 1'000;
int dp[MAX_LEN][MAX_LEN];

void set_val(int i, int j, int inc) {
    int val = inc;
    if (i > 0 && j > 0) {
        int tmp = dp[i-1][j-1] + inc;
        val = max(val, tmp);
    }
    if (i > 0) {
        int tmp = dp[i-1][j] + inc;
        val = max(val, tmp);
    }
    if (j > 0) {
        int tmp = dp[i][j-1] + inc;
        val = max(val, tmp);
    }
    dp[i][j] = val;
}

int lcs(const string& x, const string& y) {
    int lx = x.length();
    int ly = y.length();
    for (int i=0; i<lx; ++i) {
        bool isalreadyinc = false;
        for (int j=0; j<ly; ++j) {
            if (x[i] == y[j] && !isalreadyinc) {
                set_val(i, j, 1);
                isalreadyinc = true;
            } else {
                set_val(i, j, 0);
            }
        }
    }
    return dp[lx-1][ly-1];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    string x, y;
    FOR(i,0,n) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }

    return 0;
}

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

#define MAX 1001

using namespace std;

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; 
	}
}


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

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 = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (int i = 0; i <= m; ++i){
        c[i][0] = 0;
    }
    for (int i = 0; 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;
    }

        return 0;
}

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 = Y.size();
    int maxl = 0;
    X = ' ' + X;//在X[0]中插入空格
    Y = ' ' + Y;//在Y[0]中插入空格
    for(int i=0;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>
#include<string>
using namespace std;

int main(){
  int i,j,n,c[1001][1001];
  string x,y;
  cin >> n;
  while(n>0){
    n--;
    cin >> x;
    cin >> y;

    int lenx = x.size();
    int leny = y.size();

    for(i = 1; i <= lenx; i++){
      c[i][0] = 0;
    }
    for(j = 1; j <= leny; j++){
      c[0][j] = 0;
    }
  
    for(i = 1; i <= lenx; i++){
      for(j = 1; j <= leny; j++){
	if(x[i-1]==y[j-1]){
	  c[i][j] = c[i-1][j-1] + 1;
	}else if(c[i-1][j] >= c[i][j-1]){
	  c[i][j] = c[i-1][j];
	}else{
	  c[i][j] = c[i][j-1];
	}
      }
    }
    cout << c[lenx][leny] << endl;
  }
  return 0;
}

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



int solve(string x, string y) {
  int xl = x.size();
  int yl = y.size();
  int lcs[MAX + 1][MAX + 1];

  int i, j;
  for (i = 0; i < xl; i++)
    lcs[i][0] = 0;
  for (j = 0; j < yl; j++)
    lcs[0][j] = 0;

  // shift
  x = " " + x;
  y = " " + y;

  int maxlcs = 0;
  for (i = 1; i <= xl; i++) {
    for (j = 1; j <= yl; j++) {
      if (x[i] == y[j]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else {
        int a = lcs[i][j - 1];
        int b = lcs[i - 1][j];
        lcs[i][j] = max(a, b);
      }
      maxlcs = max(maxlcs, lcs[i][j]);
    }
  }
  return maxlcs;
}

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

C++: #include<bits/stdc++.h>
using namespace std;
using 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 (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

// LCSセット
const int S_LEN_MAX = 1500;
ll dp[S_LEN_MAX][S_LEN_MAX];
ll LCS(string s, string t){
    // reset
    FOR(i, 0, S_LEN_MAX){
        FOR(j, 0, S_LEN_MAX){
            dp[i][j] = 0;
        }
    }

    ll n = s.size();
    ll m = t.size();
    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]);
            }
        }
    }
    return dp[n][m];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll Q;
    cin >> Q;

    while(Q--){
        string s, t; cin >> s >> t;
        ll a = LCS(s, t);
        p(a);
    }
    
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 1000 + 5;
int dp[maxn][maxn];
char x[maxn];
char y[maxn];

int main()
{
    int q;
    scanf("%d", &q);
    while (q--)
    {
        scanf("%s %s", x + 1, y + 1);
        memset(dp, 0, sizeof(dp));
        int m = strlen(x + 1);
        int n = strlen(y + 1);
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
            {
                if (x[i] == y[j])
                    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[m][n]);
    }
    return 0;
}