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>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
int main(){
    int q;
    scanf("%d",&q);
    string x,y;
    REP(i,q){
        cin>>x>>y;
        int dp[x.size()+1][y.size()+1];
        fill(dp[0],dp[x.size()],-1);
        REP(j,x.size()+1){
            REP(k,y.size()+1){
                if(j==0||k==0) dp[j][k]=0;
                else if(x[j]==y[k]) dp[j][k]=dp[j-1][k-1]+1;
                else dp[j][k]=max(dp[j][k-1],dp[j-1][k]);
            }
        }
        printf("%d\n",dp[x.size()][y.size()]);
    }
    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int dat[1024][1024];
int getLcbLen(char* x, int xe, char* y, int ye){
	if(xe <= 0 || ye <= 0) return 0;
	if(dat[xe][ye] != -1) return dat[xe][ye];
	return dat[xe][ye] = (x[xe-1] == y[ye-1] ? 1 + getLcbLen(x, xe-1, y, ye-1) : max(getLcbLen(x, xe-1, y, ye), getLcbLen(x, xe, y, ye-1)));
}

int main() {
	int set;
	scanf("%d", &set);
	
	while(set--){
		char x[1024], y[1024];
		scanf("%s %s", x, y);
		int xe = (int)strlen(x), ye = (int)strlen(y);
		
		for(int i = 0; i < xe; i++){
			for(int k = 0; k < ye; k++){
				dat[i][k] = -1;
			}
		}
		printf("%d\n", getLcbLen(x, xe, y, ye));
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <cmath>
#include <sstream>
#define F first
#define S second
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
#define repp(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
typedef pair<double, double> pdd;
typedef pair<int,int> pii;
const double EPS = 10e-6;
int c[2000][2000]={};
string x,y;
int dp(int o,int p)
{
	if(o==x.size()||p==y.size())return 0;
	if(c[o][p]!=-1)return c[o][p];
	int ans =0;
	if(x[o]==y[p])
	{
		ans=max(dp(o+1,p+1)+1,max(dp(o+1,p),dp(o,p+1)));
	}
	else ans=max(dp(o+1,p),dp(o,p+1));

	return c[o][p]=ans;
}

int main ()
{
	int n;
	cin>>n;
	rep(i,n)
	{
		
		cin>>x>>y;
		memset(c, -1, sizeof(c));
		cout<<dp(0,0)<<endl;;
	}



}

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

int main() {
	long long Q; cin >> Q;
	for (long long i = 0; i < Q; i++) {
		string X, Y; cin >> X >> Y;
		long long X_size = X.size(), Y_size = Y.size();
		vector<vector<long long>>DP(1100, vector<long long>(1100, 0));
		for (long long j = 0; j < X_size; j++) {
			DP.at(j).at(0) = 0;
		}
		for (long long j = 0; j < Y_size; j++) {
			DP.at(0).at(j) = 0;
		}
		for (long long j = 0; j < X_size; j++) {
			for (long long k = 0; k < Y_size; k++) {
				if (X.at(j) == Y.at(k)) DP.at(j + 1).at(k + 1) = max({ DP.at(j).at(k) + 1, DP.at(j).at(k + 1), DP.at(j + 1).at(k) });
				else DP.at(j + 1).at(k + 1) = max(DP.at(j).at(k + 1), DP.at(j + 1).at(k));
			}
		}
		cout << DP.at(X_size).at(Y_size) << endl;
	}
}

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

using namespace std;

int main(){

  int q;

  cin >> q;


  for(int k = 0; k < q; k++){
    string a, b;
    cin >> a >> b;
    int dp[a.size()][b.size()] = {};
    //初期設定
    if(a[0] == b[0]){
      dp[0][0] = 1;
    }
    for(int i = 1; i < a.size(); i++){
      if(a[i] == b[0] || dp[i-1][0] == 1){
        dp[i][0] = 1;
      }
    }
    for(int j = 1; j < b.size(); j++){
      if(a[0] == b[j] || dp[0][j-1] == 1){
        dp[0][j] = 1;
      }
    }
    for(int i = 1; i < a.size(); i++){
      for(int j = 1; j < b.size(); j++){
        if(a[i] == b[j]){
          if(dp[i-1][j-1] >= dp[i-1][j] && dp[i-1][j-1] >= dp[i][j-1]){
            dp[i][j] = dp[i-1][j-1]+1;

          }
          else{
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
          }
        }
        else{
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[a.size()-1][b.size()-1] << "\n";
  }

  return 0;
}

C++: #include <iostream>

using namespace std;

int LCS[1001][1001];

int lcs(string X, string Y)
{
    int m = X.size();
    int n = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++)
        LCS[i][0] = 0;
    for(int j = 1; j <= n; j++)
        LCS[0][j] = 0;
    
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(X[i] == Y[j])
                LCS[i][j] = LCS[i - 1][j - 1] + 1;
            else
                LCS[i][j] = max(LCS[i][j - 1], LCS[i - 1][j]);
        }
    }
    return LCS[m][n];
}

int main()
{
    int q;
    cin >> q;
    string X;
    string Y;
    for(int i = 0; i < q; i++)
    {
        cin >> X;
        cin >> Y;
        cout << lcs(X, Y) << endl;
    }
}

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 = 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 q;
  cin >> q;

  for ( int i = 0; i < q; i++ ) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }

  return 0;
}