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 <iostream>
#include <string>
using namespace std;

int lcs(string X, string Y) {
	int dp[1000 + 1][1000 + 1];
	int max = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (unsigned int i = 1; i <= X.size() - 1; i++)
		dp[i][0] = 0;

	for (unsigned int i = 1; i <= Y.size() - 1; i++)
		dp[0][i] = 0;
	for (unsigned int i = 1; i <= X.size() - 1; i++) {
		for (unsigned int j = 1; j <= Y.size() - 1; j++) {
			if (X[i] == Y[i])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else if (dp[i - 1][j] >= dp[i][j - 1])
				dp[i][j] = dp[i - 1][j];
			else
				dp[i][j] = dp[i][j - 1];

			max = (max < dp[i][j]) ? dp[i][j] : max;
		}
	}

	return max;
}

int main() {
	int N;
	cin >> N;

	for (int i = 0; i < N; i++) {
		string X, Y;
		cin >> X >> Y;

		cout << lcs(X, Y) << endl;
	}

	return 0;
}

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

int main() {
  int q; cin >> q;
  string A, B;
  for (int i=0; i<q; i++) {
    cin >> A >> B;
    vector<vector<int>> LCS(A.length()+1, vector<int>(B.length()+1,0));
    for (int j=1; j<=A.length(); j++) {
      for (int k=1; k<=B.length(); k++) {
        if(A[j]==B[k]) LCS[j][k]=LCS[j-1][k-1]+1;
        else LCS[j][k]=max(LCS[j-1][k],LCS[j][k-1]);
      }
    }
    cout << LCS[A.length()][B.length()] << "\n";
  }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
char A[N],B[N];
int dp[N][N];
int main(){
    int n;
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        gets(A+1);  //从下标为1开始读入
        gets(B+1);
    int lenA = strlen(A+1);
    int lenB = strlen(B+1);
    for(int i=0;i<=lenA;i++){
        dp[i][0] = 0;
    }
    for(int i=0;i<lenB;i++){
        dp[0][i] = 0;
    }
    for (int i=1; i<=lenA; i++) {
        for(int j=1;j<=lenB;j++){
            if(A[i] == B[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[lenA][lenB]);
}
    return 0;
}

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

int lcs(string s, string t);
int main(){
  int n; cin >> n;
  string s, t;
  int ans[n];
  for(int i=0; i<n; i++){
    cin >> s >> t;
    ans[i]=lcs(s,t);
  }

  for(int i=0; i<n; i++) cout << ans[i] << endl;

  return 0;
}

int lcs(string s, string t){
  int ans=0;
  int sl=s.length(), tl=t.length();
  int dp[sl+1][tl+1];

  for(int i=0; i<=sl; i++) dp[i][0]=0;
  for(int j=0; j<=tl; j++) dp[0][j]=0;

  s=" "+s;
  t=" "+t;
  for(int i=1; i<=sl; i++){
    for(int j=1; j<=tl; j++){
      if(i==0 || j==0) dp[i][j]=0;
      else if(s[i]==t[j]) dp[i][j]=dp[i-1][j-1]+1;
      else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
    }

  }

  return dp[sl][tl];
}

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

int lcs(string X, string Y) {
  int dp[1000 + 1][1000 + 1];
  int max = 0;
  int m = X.size();
  int n = Y.size();
  X = ' ' + X;
  Y = ' ' + Y;
  for (unsigned int i = 1; i <= m; i++)
    dp[i][0] = 0;

  for (unsigned int i = 1; i <= n; i++)
    dp[0][i] = 0;
  for (unsigned int i = 1; i <= m; i++) {
    for (unsigned int j = 1; j <= n; j++) {
      if (X[i] == Y[i])
	dp[i][j] = dp[i - 1][j - 1] + 1;
      else if (dp[i - 1][j] >= dp[i][j - 1])
	dp[i][j] = dp[i - 1][j];
      else
	dp[i][j] = dp[i][j - 1];

      max = (max < dp[i][j]) ? dp[i][j] : max;
    }
  }

  return max;
}

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    string X, Y;
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
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.insert(0, " ");  // X[0]に空白を挿入
    Y.insert(0, " ");;  // Y[0]に空白を挿入

    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]);
            // if (c[i][j] > maxl) {
            //     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], m=X.size(), n=Y.size(), LMAX=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]);
      }
      LMAX=max(LMAX, c[i][j]);
    }
  }

  return LMAX;
}

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