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 <iostream>
#include <cstdio>
#include <string>

int LCS(std::string str1, std::string str2, int i, int j){
  if(str1[i] == str2[j]){
    return 1;
  }
  else{
    return 0;
  }
}

int max(int a, int b, int c){
  int m = a;
  if(b > m){
    m = b;
  }
  if(c > m){
    m = c;
  }
  return m;
}

int main(){
  int q;
  std::cin >> q;
  int ans[q];
  std::string str1, str2;
  for(int z=0; z<q; z++){
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1+1][l2+1] = {};

    for(int i=1; i<=l1; i++){
      for(int j=1; j<=l2; j++){
	DP[i][j] = max(DP[i-1][j], DP[i][j-1], DP[i-1][j-1] + LCS(str1, str2, i-1, j-1));
      }
    }
    ans[z] = DP[l1][l2];
  }

  for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int Max(int &a,int b){return a=max(a,b);}

int LCS(string &a,string &b){
  int n = a.size();
  int m = b.size();
  vector <vector<int> > dp(n+1,vector<int>(m+1));

  for(int i=0;i<n;i++)
    for(int j=0;j<m;j++){
      if(a[i] == b[j]) Max(dp[i+1][j+1],dp[i][j]+1);
      Max(dp[i+1][j],dp[i][j]);
      Max(dp[i][j+1],dp[i][j]);
    }
  return dp[n][m];
}

int main(){
  int q;
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    cout<<LCS(a,b)<<endl;
  }
  return 0;
}

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

int main(){
  int q;
  cin >> q;
  for(int m=0; m<q; m++){
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1000][1000]={0};
    
    for(int j=0; j<wordy.size(); j++){
      for(int i=0; i<wordx.size(); i++){
	if(wordx[i]==wordy[j]){
	  L[i][j] = 1+L[i-1][j-1];
	}
	else{
	  L[i][j] = max(L[i][j-1], L[i-1][j]);
	}
      }
    }
    cout << L[wordx.size()-1][wordy.size()-1] << endl;
    
    
  }
}

C++: #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> P;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
int main(void) {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string one, two;
    cin >> one >> two;
    int dp[1002][1002];
    rep(i, one.size()) {
      rep(j, two.size()) {
        if (one[i] == two[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]);
        }
      }
    }

    cout << dp[one.size()][two.size()] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>


constexpr int MAXLENGTH = 1000;
int C[MAXLENGTH + 1][MAXLENGTH + 1];
int lcs(std::string lhs, std::string rhs) {
    int lnum = lhs.length();
    int rnum = rhs.length();
    for (int i = 0; i <= lnum; i++) {
        C[i][0] = 0;
    }
    for (int i = 0; i <= rnum; i++) {
        C[0][i] = 0;
    }
    for (int il = 1; il <= lnum; il++) {
        for (int ir = 1; ir <= rnum; ir++) {
            if (lhs[il-1] == rhs[ir-1]) {
                C[il][ir] = C[il - 1][ir - 1] + 1;
            }else if(C[il - 1][ir] >= C[il][ir - 1]) {
                C[il][ir] = C[il-1][ir];
            }else{
                C[il][ir] = C[il][ir - 1];
            }
        }
    }
    return C[lnum][rnum];
}

int main() {
    int n;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string l, r;
        std::cin >> l;
        std::cin >> r;
        std::cout << lcs(l, r) << std::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; // X[0]に空白を挿入
    Y = ' ' + Y; // Y[0]に空白を挿入
    for(int i=0; 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(int argc, char const *argv[])
{
    string s1, s2;
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <utility>
#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define mod 1000000007
using namespace std;

int main(){
	int q;
	cin >> q;
	string X[q],Y[q];
	rep(i,0,q){
		cin >> X[i];
		cin >> Y[i];
	}

	rep(i,0,q){
		int m = X[i].length();
		int n = Y[i].length();
		int dp[m+1][n+1] = {};
		int maxl=0;
		rep(j,0,m){
			rep(k,0,n){
				if(X[i][j] == Y[i][k]) dp[j+1][k+1] = dp[j][k]+1;
				else dp[j+1][k+1] = max(dp[j][k+1],dp[j+1][k]);
			}
		}
		cout << dp[m][n] << endl;
	}
	return 0;
}

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

int z[1001][1001];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string x,y;
    int n;
    cin>>n;
    while(n--) {
        cin>>x>>y;
        int lx=x.size(), ly=y.size();
        for(int i=0; i<=lx; i++) z[i][0]=0;
        for(int i=0; i<=ly; i++) z[0][i]=0;
        for(int i=1; i<=lx; i++) {
            for(int j=1; j<=ly; j++) {
                if(x[i-1]==y[j-1]) z[i][j]=z[i-1][j-1]+1;
                else z[i][j]=max(z[i][j-1],z[i-1][j]);
            }
        }
        cout<<z[lx][ly]<<'\n';
    }
    return 0;
}