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 <vector>
#include <string>
#include <algorithm>

using namespace std;

// typedef
typedef long long ll;

// container util
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

// repetition
#define FOR(i,m,n) for(ll i = ((ll) m); i < ((ll) n); ++i)
#define REP(i,n) FOR(i,0,n)

int solve(const string& X, const string& Y) {
    int xlen = X.length()+1, ylen = Y.length()+1;
    int* dp = new int[xlen*ylen];
    for(int i = 0; i < xlen; i++) {
        for(int j = 0; j < ylen; j++) {
            if(i == 0 || j == 0) {
                dp[i*ylen+j] = 0;
                continue;
            }
            dp[i*ylen+j] = max(
                dp[(i-1)*ylen+(j-1)] + (X[i-1] == Y[j-1]? 1 : 0),
                max(dp[(i-1)*ylen+j], dp[i*ylen+(j-1)])
            );
        }
    }
    int ret = dp[xlen*ylen - 1];
    delete[] dp;
    return ret;
}

int main() {
    int q; cin >> q;
    REP(i, q) {
        string X, Y;
        cin >> X >> Y;
        cout << solve(X, Y) << endl;
    }
    return 0;
}

/*
Longest Common Subsequence
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=jp

dp[i][j] = X1...XiとY1...Yjの最長共通部分列
dp[i][j] = 
dp[i-1][j-1] + 1            if Xi == Yj
max(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) otherwise
*/

C++: #include<algorithm>
#include<iostream>
#include<cstring>
#define rep(i,a) for(int i=0;i<(a);++i)
#define repd(i,a) for(int i=(a);i>=0;--i)
#define clr(a,v) memset((a),(v),sizeof(a))

const int MAX_X = 1000, MAX_Y = 1000;

int q;
std::string X, Y;
int dp[MAX_X+1][MAX_Y+1];

int main()
{
    std::cin >> q;
    while( q-- )
    {
        std::cin >> X >> Y;

        clr( dp, 0 );

        repd( i, X.size()-1 ) repd( j, Y.size()-1 )
        {
            if( X[i] == Y[j] )
                dp[i][j] = dp[i+1][j+1]+1;
            else
                dp[i][j] = std::max( dp[i+1][j], dp[i][j+1] );
        }

        printf( "%d\n", dp[0][0] );
    }

    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    for(i = 1 ; i <= m ; i++ ) {
        for(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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        printf("%d\n", lcs(x,y));
    }

    return 0;
}

C++: // LCSの練習

#include"bits/stdc++.h"
#include <iostream>
#include <vector>


using namespace std;


int main(){
	
	int q;

	cin >> q ;

	vector<int> ans;

	for(int i= 0; i < q; i++){
		
		string x,y;

		cin >> x;
		cin >> y;

		int len_x = (int)x.length();
		int len_y = (int)y.length();

		// int dp[len_x+1][len_y+1];

		vector<vector<int> > dp(len_x + 1, vector<int>(len_y + 1));

		for(int i = 1; i <= len_x; i++){
			for(int j = 1; j <= len_y; j++){
				if(x[i-1] == y[j-1]){
					dp[i][j] = dp[i-1][j-1]+1;
				}
				else{
					dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 
				}
			}
		}

		ans.push_back(dp[len_x][len_y]);
		// cout << dp[len_x][len_y] << endl;

	}

	for(int i = 0; i < q; i++){
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>

int lcs(std::string x, std::string y)
{
  int m = x.length();
  int n = y.length();
  int cs[m + 1][n + 1];

  for (int i = 1; i < m + 1; ++i)
    cs[i][0] = 0;
  for (int j = 1; j < n + 1; ++j)
    cs[0][j] = 0;

  for (int i = 1; i < m + 1; ++i)
  {
    for (int j = 1; j < n + 1; ++j)
    {
      if (x[i - 1] == y[j - 1])
        cs[i][j] = cs[i - 1][j - 1] + 1;
      else
        cs[i][j] = std::max(cs[i - 1][j], cs[i][j - 1]);
    }
  }

  return cs[m][n];
}

int main()
{
  int q;
  std::cin >> q;

  std::string x, y;
  for (int i = 0; i < q; ++i)
  {
    std::cin >> x >> y;
    std::cout << lcs(x, y) << std::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;  // X[0]に空白を挿入
  Y = ' ' + Y;  // X[0]に空白を挿入
  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 <= 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;
}