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

}