Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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

C++: /**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t Q;
  cin >> Q;
  for (int64_t q = 0; q < Q; ++q) {
    string s, t;
    cin >> s >> t;
    const int64_t SL = static_cast<int64_t>(s.length());
    const int64_t TL = static_cast<int64_t>(t.length());
    vector<vector<int64_t>> dp(SL + 1, vector<int64_t>(TL + 1, 0));
    for (int64_t i = 1; i <= SL; ++i) {
      for (int64_t j = 1; j <= TL; ++j) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
        }
      }
    }
    cout << dp[SL][TL] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<map>

using namespace std;

const int MAX_SIZE = 1000;

int LCS(string& X, string& Y,int dp[MAX_SIZE][MAX_SIZE], int X_pos, int Y_pos) {
	if (Y_pos == Y.length() || X_pos == X.length()) {
		return 0;
	}
	int res;

	if (dp[X_pos][Y_pos + 1] == -1) {
		dp[X_pos][Y_pos + 1] = LCS(X, Y, dp, X_pos, Y_pos + 1);
	}
	res = dp[X_pos][Y_pos + 1];

	for (int i = X_pos; i < X.length(); i++) {
		if (X[i] == Y[Y_pos]) {
			if (dp[i + 1][Y_pos + 1] == -1) {
				dp[i + 1][Y_pos + 1] = LCS(X, Y, dp, i + 1, Y_pos + 1);
			}
			res = max(dp[i + 1][Y_pos + 1] + 1, res);
		}
	}
	return res;
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string X, Y;
		int dp[MAX_SIZE][MAX_SIZE];

		cin >> X;
		cin >> Y;

		for (int i = 0; i < X.length() + 1; i++) {
			for (int j = 0; j < Y.length() + 1; j++) {
				dp[i][j] = -1;
			}
		}

		cout << LCS(X, Y, dp, 0, 0) << endl;
	}
	return 0;
}

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  ans[l]=N[X.size()][Y.size()];
  l++;
  // cout<<N[X.size()][Y.size()]<<endl;
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }
  cout<<ans[0]<<endl;
 cout<<ans[1]<<endl;
 cout<<ans[2]<<endl;
}

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

int preX[1020][30];
int dp[1020];

int main() {
	int q;
	cin >> q;
	for(int t = 0; t < q; t++) {
		string X, Y;
		for(int i = 0; i < 1020; i++) {
			for(int j = 0; j < 30; j++) {
				preX[i][j] = 0;
			}
			dp[i] = 0;
		}
		cin >> X >> Y;
		for(int i = 0; i < X.size(); i++) {
			for(int j = 0; j < 30; j++) {
				preX[i+1][j] = preX[i][j];
			}
			if(i > 0)
				preX[i+1][X[i-1]-'a'] = i;
		}
		for(int i = 0; i < Y.size(); i++) {
			for(int j = (int)X.size()-1; j >= 0; j--) {
				if(Y[i] == X[j])
					for(int k = 0; k < 30; k++)
						dp[j+1] = max(dp[j+1], dp[preX[j+1][k]] + 1);
			}
		}
		int res = 0;
		for(int i = 0; i < 1020; i++)
			res = max(res, dp[i]);
		cout << res << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef complex<double> P;

const int MAX_SIZE = 1001;
int q;
int dp[MAX_SIZE][MAX_SIZE] = {};

int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {

    for (int j = 0; j < MAX_SIZE; j++) {
      dp[MAX_SIZE-1][0] = 0;
      dp[0][MAX_SIZE-1] = 0;
    }

    string a, b; cin >> a >> b;

    for (int j = 0; j < a.size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        if(a[j] == b[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[a.size()][b.size()] << endl;
  }
}