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 <vector>
#include <iostream>
              
void solve() {
  std::string X, Y;
  std::cin >> X >> Y;
  const int LX = X.size();
  const int LY = Y.size();
  std::vector<int> dp0(LY+1, 0), dp1(LY+1, 0);
  std::vector<int> *dp = &dp0;
  std::vector<int> *dp_ = &dp1;
  for (int i = 1; i < LX+1; i++) {
    std::swap(dp, dp_);
    for (int j = 1; j < LY+1; j++) {
      (*dp)[j] = std::max((*dp)[j-1], (*dp_)[j]);
      if (X[i-1] == Y[j-1]) {
        (*dp)[j] = std::max((*dp)[j], (*dp_)[j-1] + 1);
      }       
    }         
  }           
  std::cout << dp->back() << std::endl;
}             
              
int main() {  
  int N;      
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    solve();  
  }           
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#define MAX 1000


int lcs(std::string X, std::string Y) {
    int c[MAX+1][MAX+1];
    int maxl = 0;
    int m = X.size();
    int n = Y.size();
    for (int i = 1; i <= m; i++) {
        c[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
        c[0][i] = 0;
    }
    X = ' ' + X;
    Y = ' ' + Y;
    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] = std::max(c[i-1][j], c[i][j-1]);
            }
            maxl = std::max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main() {
    std::string s1, s2;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        std::cin >> s1 >> s2;
        int a = lcs(s1, s2);
        printf("%d\n", lcs(s1, s2));
    }
    
    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; // 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() {
	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<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define pd(a,n) for(int i=0;i<n;i++)cout<<a[i]<<((i==n-1)?"\n":" ");cout<<endl;
#define pdd(a,n,m)for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define mp make_pair
#define pb push_back
#define ll long long
//#define __int64 long long
#define P pair<int,int>
#define PP pair<P,int>

int dp[1011][1011];

int query(string a,string b){
  int n=a.size();
  int m=b.size();

  rep(i,1011)rep(j,1011)dp[i][j]=0;

  rep(i,n){
    rep(j,m){
      if(a[i]==b[j]){
        dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
      }
      dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
      dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
    }
  }
  int ret=0;
  rep(i,n+1)rep(j,m+1)ret=max(ret,dp[i][j]);

  return ret;
}
int main(){
  int q;
  cin>>q;
  rep(i,q){
    string a,b;
    cin>>a>>b;
    cout<<query(a,b)<<endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
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 = 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(void)
{
    string X, Y;
    int n; cin >> n;

    for (int i = 0; i < n; ++i ) {
        cin >> X >> Y;
        cout << LCS(X, Y) << endl;
    }
    return 0;
}

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

string s, t;
int dp[100][100];

void solve() {
    cin >> s >> t;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < t.size(); j++) {
            dp[i][j] = (i < 1 || j < 1 ? 0 : dp[i-1][j-1]) + (s[i] == t[j] ? 1 : 0);
            int a = (i < 1 ? 0 : dp[i-1][j]);
            int b = (j < 1 ? 0 : dp[i][j-1]);
            dp[i][j] = max(dp[i][j], max(a, b));
        }
    }

    cout << dp[s.size()-1][t.size()-1] << endl;
}

int main() {
    int n;
    cin >> n;
    while (n--) solve();

    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i,n)   for (int i = 0; i < (n); ++i)
#define SORT(a)    sort((a).begin(), (a).end());
#define UNIQ(a)    SORT(a);(a).erase(unique((a).begin(), (a).end()), (a).end());
#define FIND(a,x)  find((a).begin(), (a).end(), (x)) != (a).end()
#define REVERSE(a) reverse((a).begin(), (a).end());

using namespace std;
using ll = long long;
using P = pair<int,int>;

const int MOD = 1000000007; // 10^9 + 7

int lcs(string x, string y) {
  int m = x.size(), n = y.size();
  int dp[m + 1][n + 1]; memset(dp, 0, sizeof(dp));

  REP(i, m)REP(j, n)
    if (x[i] == y[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]);

  return dp[m][n];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));

  int t; cin >> t;
  REP(i, t) {
    string x, y; cin >> x >> y;

    cout << lcs(x, y) << endl;
  }
}

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

int main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    vector<vector<int>> dp(x.size()+1,vector<int>(y.size()+1,0));
    
    for(int xi=0;xi<x.size();++xi){
      for(int yi=0;yi<y.size();++yi){
        if(x[xi]==y[yi]){
          dp[xi+1][yi+1]=dp[xi][yi]+1;
        }else{
          if (dp[xi+1][yi]>dp[xi][yi+1]){
            dp[xi+1][yi+1]=dp[xi+1][yi];
          }else{
            dp[xi+1][yi+1]=dp[xi][yi+1];
          }
        }
      }
    }
    int ans=dp[x.size()][y.size()];
    cout << ans << endl;
  }
}