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] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// 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>
# include<algorithm>
using namespace std;
# define N 300
# define M 1000
# define L 1001

int main(){
    int q;
    cin >> q;
    string iranai; iranai = cin.get(); //改行文字を排除

    string A[N][M];
    int cnt[N];
    for(int i=0; i<(q*2); i++){
        int j=0;
        while((A[i][j] = cin.get()) != "\n"){
            j++;
            cnt[i]++;
        }
    }

    for(int k=0; k<(q*2); k+=2){
        int B[L][L];
        for(int i=0; i<=cnt[k]; i++){
            string str;
            if(i!=0) str = A[k][i-1];
            for(int j=0; j<=cnt[k+1]; j++){
                if(i==0 || j==0) B[i][j] = 0;
                else if(A[k+1][j-1] != str) B[i][j] = max(B[i-1][j], B[i][j-1]);
                else B[i][j] = B[i][j] = B[i-1][j-1] + 1;
            }
        }
        cout << B[cnt[k]][cnt[k+1]] << "\n";
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1e9;
const long long INFL = 1LL<<60;

int dp[10005][10005];

int main() {
  int n;
  cin >> n;
  rep(k, n) {
    string a, b;
    cin >> a >> b;
    int m = a.size();
    int n = b.size();
    rep(i, 10005) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < m + 1; j++) {
        if (b[i-1] == a[j-1]) {
          dp[i][j] = max(max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1] + 1);
        } else {
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[n][m] << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<iostream>
#define MAX(a,b) (a>b?a:b)
using namespace std;
const int MAXN=1010;
int dp[MAXN][MAXN];
char a[MAXN],b[MAXN];
int main()
{
    int n;
    cin>>n;
    for(int k=0; k<n; k++)
    {
        while(scanf("%s%s",a+1,b+1))
        {
            memset(dp,0,sizeof(dp));
            int i,j;
            for( i=1; a[i]; i++)
            {
                for(j=1; b[j]; j++)
                {
                    if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;
                    else dp[i][j]=MAX(dp[i][j-1],dp[i-1][j]);
                }
            }
            printf("%d\n",dp[i-1][j-1]);
        }
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  int maxl = 0;

  // 初期化、境界条件を考慮して場合分け
  // for (int i = 0; i < X.size(); i++) {
  //   if (X[i] == Y[0]) {
  //     memo[i][0] = 1;
  //   } else {
  //     memo[i][0] = 0;
  //   }
  //   maxl = max(maxl, memo[i][0]);
  // }
  //
  // for (int j = 0; j < Y.size(); j++) {
  //   if (X[0] == Y[j]) {
  //     memo[0][j] = 1;
  //   } else {
  //     memo[0][j] = 0;
  //   }
  //   maxl = max(maxl, memo[0][j]);
  // }

  // cout << memo[0][0] << endl;
  // cout << "--------------" << endl;

  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (i == 0 || j == 0) {
        if (X[i] == Y[j]) {
          memo[i][j] = 1;
        } else {
          memo[i][j] = 0;
        }
      } else {
        if (X[i] == Y[j]) {
          memo[i][j] = memo[i-1][j-1] + 1;
        } else {
          memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
        }
      }


      maxl = max(maxl, memo[i][j]);
      //cout << i << "," << j << "," << X[i] << "," << Y[j] << "," << maxl << endl;
    }
  }
  return maxl;
}

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

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

using namespace std;

#define N 1001

int lcs(string x,string y){
    int c[N + 1][N + 1] = {};
    int maxl = 0;
    x = ' ' + x; // X[0]に空白を挿入
    y = ' ' + y; // Y[0]に空白を挿入

    for(int i = 1;i <= x.size();++i){
        for(int j = 1;j <= y.size();++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]);
        }
    }
    
    for(int i = 1;i <= x.size();++i){
        for(int j = 1;j <= y.size();++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 - 1;
}

int main(){
    string s1,s2;
    int n;
    cin >> n;
    
    for(int i = 0;i < n;++i){
        cin >> s1 >> s2;
        cout << lcs(s1,s2) << 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=0; i<=m; i++) c[i][0] = 0;
	for (int j=0; j<=n; j++) c[0][j] = 0;

	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			if (X[i] == Y[j]){
				c[i][j] = c[i-1][j-1] + 1;
			} else{
				c[i][j] = max(c[i][j-1], c[i-1][j]);
			}
			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;

}