Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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