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++: #define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cfloat>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <functional>
#include <stdexcept>

typedef long long ll;
typedef unsigned int ui;
typedef unsigned short us;
typedef unsigned long long ull;

template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<std::vector<T>>& v) {
  if (!v.empty()) {
    for (std::vector<T> v2 : v) {
      std::copy(v2.begin(), v2.end(), std::ostream_iterator<T>(out, ", "));
      out << "\b\b \n";
    }
  }
  return out;
}
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
  if ( !v.empty() ) {
    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b ";
  }
  return out;
}

using namespace std;

namespace my {
  class LCS
  {
  public:
    LCS() = default;
    ~LCS() = default;

  private:

  };
}

int main(int argc, const char * argv[]) {
  int q;
  cin >> q;
  vector<string> x;
  vector<string> y;
  for (size_t i = 0; i < q; i++)
  {
    string temp;
    cin >> temp;
    x.push_back(temp);
    cin >> temp;
    y.push_back(temp);
  }
  
  
  
  for (size_t i = 0; i < q; i++)
  {
    size_t x_length = x[i].length();
    size_t y_length = y[i].length();
    size_t max_length = min(x_length, y_length);
    vector<int> length_y_nanmoji(1, y_length);
    map<char, vector<int>> y_nanmoji;
    for (size_t j = 0; j < x_length; j++)
    {
      char x_char = x[i][j];
      if (y_nanmoji.find(x_char) == y_nanmoji.end()) {
        size_t count = 0;
        bool is_looped = false;
        do
        {
          count = y[i].find(x_char, count);
          if (count >= y_length) {
            if (!is_looped) {
              y_nanmoji[x_char].push_back(-1);
            }
            break;
          }
          else {
            y_nanmoji[x_char].push_back((int)count);
            count++;
          }
        } while (is_looped = true, true);
      }

      auto y_nanmoji_now = y_nanmoji[x_char];
      if (y_nanmoji_now[0] == -1) {
        continue;
      }
      for(int l = (int)y_nanmoji_now.size() - 1; l >= 0; l--)
      {
        int val = y_nanmoji_now[l];
        bool is_done = false;
        for (int k = 0; k < (int)length_y_nanmoji.size(); k++)
        {
          if (length_y_nanmoji[k] >= val) {
            length_y_nanmoji[k] = val;
            is_done = true;
            break;
          }
        }
        if (!is_done) {
          length_y_nanmoji.push_back(val);
        }
      }

    }
    int kotae = length_y_nanmoji[0] >= y_length ? 0 : length_y_nanmoji.size();
    cout << kotae << 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 = 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>
#include <string>
#include <cstring> //memset(dp,0,sizeof(dp))
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define repp(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
//小数点以下　cout << fixed << setprecision(10) << num << endl;
//桁数はlog10(mid)でやれ　log(mid)/log(10)はWA
//long*long<0で判定するな　オーバーフローする
//開区間と閉区間どうにかしろ
//アルファベットが何番目かは x=s[i]-'a'
const ll MOD = 1000000007;

int LCS(string x,string y){
    int nx=x.size();
    int ny=y.size();
    int dp[nx][ny];
    //dp[i][j]:=xを上からi個、yを上からj個見たときの最長部分文字列の長さ
    memset(dp,0,sizeof(dp));
    if(x[0]==y[0]) dp[0][0]=1;
    else dp[0][0]=0;
    //dp[i+1][j]= (if x[i+1]==y[j]:dp[i][j]+1 else dp[i][j])
    rep(i,nx){
        rep(j,ny){
            if(x[i]==y[j]){
                if(i>0 && j>0) dp[i][j]=dp[i-1][j-1]+1;
                else dp[i][j]=1;
            }
            else if(i>0 && j>0) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            else if(i>0) dp[i][j]=dp[i-1][j];
            else if(j>0) dp[i][j]=dp[i][j-1];
        }
    }
    return dp[nx-1][ny-1];
}

int main(){
    int q;
    cin >> q;
    rep(i,q){
        string x,y;
       cin >> x >> y;
        cout << LCS(x,y) << endl;
    }
}