Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n);++i)
using namespace std;
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 long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1,0,-1,0};int dy[4] = {0,-1,0,1};

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

C++: #include <iostream>
#include <cstring>

using namespace std;

int main(void)
{
    int q;
    char x[1001], y[1001];
    int table[1001][1001];
    
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        int xl = strlen(x), yl = strlen(y);

        for (int i = 0; i < xl; i++)
            for (int j = 0; j < yl; j++)
                if (x[i] == y[j])
                    table[i+1][j+1] = 1 + table[i][j];
                else
                    table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);

        cout << table[xl][yl] << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

int table[1001][1001];

int lcs(char *x, char *y){
    int i, j, lx, ly;
    lx = strlen(x+1);
    ly = strlen(y+1);
    for(i=1;i<=lx;i++){
        for(j=1;j<=ly;j++){
            if(x[i]==y[j])table[i][j] = table[i-1][j-1] + 1;
            else table[i][j] = std::max(table[i][j-1],table[i-1][j]);
        }
    }
    return table[lx][ly];
}

int main(){
    int i, j, q;
    char x[1002],y[1002];
    scanf("%d",&q);
    for(i=0;i<1001;i++){
        table[i][0] = 0;
    }
    for(j=0;j<1001;j++){
        table[0][j] = 0;
    }
    for(i=0;i<q;i++){
        scanf("%s %s",x+1,y+1);
        printf("%d\n",lcs(x,y));
    }
    return 0;
}

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

int q;
int dp[1001][1001];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( ret, std::max( rec( i+1, j ), rec( i, j+1 ) ) );

  return ret;
}

int main()
{
  std::cin >> q;
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define _GLIBCXX_DEBUG

void printv(vector<int> v) {
  for (int i : v)
    cout << i << " ";
  cout << endl;
}

int main() {
  int q;
  cin >> q;
  rep(loop, q) {
    string x, y;
    cin >> x >> y;
    vector<vector<int>> L(1010, vector<int>(1010, 0));
    int lx = x.size(), ly = y.size();
    for (int i = 1; i <= lx; i++) {
      for (int j = 1; j <= ly; j++) {
        if (x[i-1] == y[j-1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else
          L[i][j] = max(L[i - 1][j], L[i][j - 1]);
      }
    }
    cout << L[lx][ly] << endl;
  }
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>

#define debug(x) cout << #x << "==" << x << endl;

using namespace std;

//??\???
#define MAX_LEN 1000
int N;
int dp[MAX_LEN + 1][MAX_LEN + 1];

void print_dp_table(int n, int m) {
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cout <<setw(2) << dp[i][j];
    }
    cout << "\n";
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;

  string s1, s2;

  while( cin >> s1 >> s2 ) {

    std::vector<char> a;
    std::vector<char> b;
    for(char& c : s1) {
      a.push_back(c);
    }
    for(char& c : s2) {
      b.push_back(c);
    }


    // dp[i][j] := a???i???????????§???b???j???????????§????????¨??????????????±?????¨??????
    // dp ????????????????????????
    fill(dp[0], dp[MAX_LEN + 1], 0);

    for(int i = 1; i <= (int)a.size(); i++ ) {
      for(int j = 1; j <= (int)b.size(); j++) {
        if( a[i-1] == b[j-1] ) {
          dp[i][j] = dp[i-1][j-1] + 1;
        } else {
          dp[i][j] = max( dp[i-1][j], dp[i][j-1] );
        }
      }
    }

    // for( int i = 0; i < (int)a.size(); i++) {
    //   for( int j = 0; j < (int)b.size(); j++) {
    //     if( a[i] == b[j] ) {
    //       dp[i + 1][j + 1] = dp[i][j] + 1;
    //     } else {
    //       dp[i + 1][j + 1] = max( dp[i + 1][j], dp[i][j + 1]);
    //     }
    //   }
    // }

    // print_dp_table(a.size(), b.size());
    cout << dp[a.size()][b.size()] << endl;

  }

  return 0;
}

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

static const int N = 100;

int main() {
	int n, p[N + 1], m[N + 1][N + 1];
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> p[i - 1] >> p[i];
	}

	for (int i = 1; i <= n; i++)m[i][i] = 0;
	for (int l = 2; l <= n; l++) {
		for(int i=1;i<=n-l+1;i++){
		int j = i + l - 1;
		m[i][j] = (1 << 21);
		for (int k = i; k <= j - 1; k++) {
			m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}

	cout << m[1][n] << 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;
}