Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <chrono>
#define lint long long int
#define FOR(x, to) for(int x=0; x<(int)(to); x++) // for簡略表記
#define DUMP(ar) for(int loop=0; loop<(int)ar.size(); loop++) cout << "[" << loop << "]:" << ar[loop] << endl // 配列一覧表示
#define DUMPL(ar) for(int loop=0; loop<(int)ar.size(); loop++) { cout << setw(2) << ar[loop]; if(loop<(int)ar.size()-1) cout << ' '; } cout << endl; // 配列一行一覧表示
#define COUT(x) cout << x << endl // 単要素出力
#define ALL(ar) ar.begin(), ar.end() // コンテナの全要素
#define LOOPD(i) cout << "ループ[" << i << "]回目---------------" << endl // ループ回数デバグ
using namespace std;
// cout << fixed << setprecision(4) で小数点以下4桁の桁そろえ
// cout << setw(5) << ... で表示形式を5桁に設定 5桁未満なら頭に空白がつく 5桁越なら先頭から5桁まで
// cout << setw(5) << setfill('0') << ... で空白部分の埋め文字に5を使う
// getline(cin, [変数]); で空白など(ホワイトスペース)を含む入力
// cin >> ws; でそこまでに入力ストリームに残されたホワイトスペースを取り除く(=以降のgetlineの有効化)

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int q;
    cin >> q;
    vector<string> s1(q), s2(q);
    for(int i=0; i<q; i++){
        cin >> s1[i] >> s2[i];
    }

    vector<int> ans(q);
    for(int i=0; i<q; i++){
        int s1_size = s1[i].size();
        int s2_size = s2[i].size();
        
        vector<vector<int>> dp(s1_size, vector<int>(s2_size, 0));
        // 初期化
        bool flag_same = false;
        for(int j=0; j<s1_size; j++){
            if(s1[i][j] == s2[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[j][0] = 1;
            else dp[j][0] = 0;
        }
        flag_same = false;
        for(int j=0; j<s2_size; j++){
            if(s2[i][j] == s1[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[0][j] = 1;
            else dp[0][j] = 0;
        }

        // 計算
        for(int j=1; j<s1_size; j++){
            for(int k=1; k<s2_size; k++){
                if(s1[i][j] == s2[i][k]){
                    dp[j][k] = min(dp[j-1][k-1], min(dp[j-1][k], dp[j][k-1])) + 1;
                }else{
                    dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
                }
            }
        }
        // LOOPD(i);
        // for(int j=0; j<s1_size; j++){
        //     DUMPL(dp[j]);
        // }
        // cout << "   ";
        // for(int j=0; j<s2_size; j++){
        //     cout << setw(2) << s2[i][j] << ' ';
        // }
        // cout << endl;

        // for(int j=0; j<s1_size; j++){
        //     cout << setw(2) << s1[i][j] << ' ';
        //     for(int k=0; k<s2_size; k++){
        //         cout << setw(2) << dp[j][k] << ' ';
        //     }
        //     cout << endl;
        // }

        ans[i] = dp.back().back();
    }
    
    // DUMP(ans);
    for(int i=0; i<q; i++) cout << ans[i] << endl;
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <string>

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    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 < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::cin >> n;
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::gets(a);
        std::gets(b);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

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

using namespace std;

string str1,str2;
int ans;

void lcs(string str1,string str2){

  int a = str1.size();
  int b = str2.size();
  int cou[1000][1000] ={{}};
  int i,j;


  for(i = 0;i < a;i++){
    for(j = 0;j < b;j++){
      
      // cout << str1[i] << ' ' << str2[j] << endl;


      if(str1[i] == str2[j]){
	cou[i][j] = cou[i-1][j-1] + 1;
      }
      else if(cou[i - 1][j] > cou[i][j - 1]){
	cou[i][j] = cou[i - 1][j];
      }
      else  cou[i][j] = cou[i][j - 1];

      // cout << "cou = " <<  cou[i][j] << endl;
      ans = cou[i][j];
    }
  }

  // return cou[i][j];

}

int main(){

  int n;
  cin >> n;
  for(int i2 = 0;i2 < n;i2++){

  cin >> str1 >> str2;

  lcs(str1,str2);

  cout << ans  << 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;
  Y=' '+Y;
  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;
}