Go: package main

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

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 {
	lenX, lenY := len(strX), len(strY)
	memo := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		memo[i] = make([]int, lenY+1)
	}

	lcs := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if strX[i-1] == strY[j-1] {
				memo[i][j] = memo[i-1][j-1] + 1
			} else {
				if memo[i-1][j] > memo[i][j-1] {
					memo[i][j] = memo[i-1][j]
				} else {
					memo[i][j] = memo[i][j-1]
				}
			}
			if memo[i][j] > lcs {
				lcs = memo[i][j]
			}
		}
	}
	return lcs
}

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"
using namespace std;

#define MAX_S 1010
#define MAX_T 1010

template<typename T> int lcs(T s,T t){
    int lens=s.size();
    int lent=t.size();
    int dp[MAX_S+1][MAX_T+1]={};
    
    
    for(int i=0;i<lens;i++){
        for(int j=0;j<lent;j++){
            if(s[i]==t[j])  dp[i+1][j+1] = dp[i][j]+1;
            else            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
        }
    }
    return dp[lens][lent];
}

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string s,t;
        cin>>s>>t;
        cout<<lcs(s,t)<<endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int solve(string d1, string d2) {
  int m = d1.size(), n = d2.size();
  vvi c(m + 1, vi(n + 1));
  d1 = " " + d1;
  d2 = " " + d2;
  
  int l = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (d1[i] == d2[j]) c[i][j] = c[i - 1][j - 1] + 1;
      else c[i][j] = max(c[i - 1][j], c[i][j - 1]);

      l = max(l, c[i][j]);
    }
  }

  return l;
}

int main() {
  int q;
  cin >> q;
  
  rep(i, q) {
    string d1, d2;
    cin >> d1 >> d2;
    cout << solve(d1, d2) << endl;
  }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------


int main() {
    int n;
    cin>>n;
    rep(i,n){
        string s1,s2;
        cin>>s1>>s2;
        
        
        vvll dp(s1.size()+1,vll(s2.size()+1));
        
        rep(i,s1.size()){
            dp.at(i).at(0)=0;
            rep(j,s2.size()){
                dp.at(0).at(j)=0;
                if(s1.at(i)==s2.at(j)){
                    dp.at(i+1).at(j+1)=(dp.at(i).at(j)+1);//,dp.at(i).at(j+1)+1);
                }else{
                    dp.at(i+1).at(j+1)=max(dp.at(i+1).at(j),dp.at(i).at(j+1));
                }
            }
        }
        cout<<dp.at(s1.size()).at(s2.size())<<endl;
    }
    
}

C++: #include <stdio.h>
#include <string.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repi(i, n) for(int i = 1; i < n; ++i)

int max(int a, int b) {
  return a > b ? a : b;
}

int main(void) {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    char x[1500], y[1500], h, w;
    scanf("%s", x);
    scanf("%s", y);
    h = strlen(x), w = strlen(y);
    int dp[h][w], len[w], maxs = 0;
    rep(j, h) rep(k, w) dp[j][k] = 0;
    rep(j, w) if(x[0] == y[j]) dp[0][j] = 1, maxs = 1;
    len[0] = 0;
    repi(j, w) len[j] = max(dp[0][j - 1], len[j - 1]);
    repi(j, h) {
      rep(k, w) if(x[j] == y[k]) {
        dp[j][k] = len[k] + 1;
        if(maxs < dp[j][k]) maxs = dp[j][k];
      }
      repi(k, w) len[k] = max(dp[j][k - 1], max(len[k], len[k - 1]));
      /*rep(k, w) printf("%d ", len[k]);
      printf("\n");*/
    }
    /*printf("\n");
    rep(j, h) {
      rep(k, w) printf("%d ", dp[j][k]);
      printf("\n");
    }*/
    printf("%d\n", maxs);
  }
  return 0;
}

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

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;

        int DP[1024][1024] = {};

        for(int i=1;i<=(int)X.size();++i){
            for(int j=1;j<=(int)Y.size();++j){
                if(X[i] == Y[j]){ // X???i???????????¨Y???j??????????????????????????????
                    DP[i][j] = DP[i-1][j-1] + 1;
                } else {
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
                }
            }
        }
        cout << DP[(int)X.size()][(int)Y.size()] << endl;
    }
}

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

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;
        int xlen = (int)X.size();
        int ylen = (int)Y.size();

        vector<vector<int> > L(xlen+1 ,vector<int>(ylen+1, 0));

        for(int i=1;i<=xlen;++i){
            for(int j=1;j<=ylen;++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[xlen][ylen] << 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 ????????????????????????
    fill(dp[0], dp[MAX_LEN + 1], 0);

    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<cstring>
using namespace std;
int main(){
  int n; cin>>n;
  for(int i=0;i<n;++i){
    char str1[1001],str2[1001]; cin>>str1>>str2;
    int len1=strlen(str1),len2=strlen(str2);
    int dp[1001][1001]={},max=0;
    for(int i=0;i<len1;++i){
      for(int j=0;j<len2;++j){
	if(str1[i]==str2[j]) dp[i+1][j+1]=dp[i][j]+1;
	else{
	  if(dp[i][j+1]<dp[i+1][j]) dp[i+1][j+1]=dp[i+1][j];
	  else dp[i+1][j+1]=dp[i][j+1];
	}
	if(max<dp[i+1][j+1]) max=dp[i+1][j+1];
      }
    }
    cout<<max<<endl;
  }
  return 0;
}