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<cstdlib>

int main(){
  int n;
  std::string x,y;
  std::cin>>n;
  int a[1000][1000];
  int b[n];

  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1000;j++){
      for(int k=0;k<1000;k++){
	a[j][k]=0;
      }
    }
    for(int j=0;j<x.size();j++){
      for(int k=0;k<y.size();k++){
	if(x[j]==y[k]){
	  a[j][k]=a[j-1][k-1]+1;
	}else{
	  if(a[j-1][k]>a[j][k-1]){
	    a[j][k]=a[j-1][k];
	  }else{
	    a[j][k]=a[j][k-1];
	  }
	}
      }
    }
    b[i]=a[x.size()-1][y.size()-1];
  }

  for(int i=0;i<n;i++){
  std::cout<<b[i]<<"\n";
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)x.size())
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi  = vector<int>;
constexpr int INF = 1001001001001001001LL;
/*
<NAME> 最長共通部分列 (Longest Common Subsequence)
<TRIGGER> LCS
<TAG> DP, etc
<*NOTE>
文字列の最長共通部分列を求める。$O(NM)$
<*CODE> */
int lcs(string a, string b) {
    int n = sz(a), m = sz(b);
    vvi dp(n + 1, vi(m + 1, 0));
    rep(i, 0, n) {
        rep(j, 0, m) {
            if (a[i] == b[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[n][m];
}

// hash : [59, 176, 206, 65, 188, 72, 103, 19]]

signed main() {
    int Q;
    cin >> Q;
    while (Q--) {
        string S, T;
        cin >> S >> T;
        cout << lcs(S, T) << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 1000
int dp[MAX + 1][MAX + 1];
int LCS(string STR1, string STR2)
{
	int length1 = STR1.size();
	int length2 = STR2.size();
	//???????????¨????????????????????????
	STR1 = ' ' + STR1;
	STR2 = ' ' + STR2;
	for (int i = 0; i < length1; i++)
	{
		for (int l = 0; l < length2; l++)dp[i][l] = 0;
	}
	for (int i = 1; i <= length1; i++)
	{
		for (int l = 1; l <= length2; l++)
		{
			if (STR1[i] == STR2[l])dp[i][l] =dp[i][l-1]+1;
			else
			{
				dp[i][l]=max(dp[i - 1][l], dp[i][l - 1]);
			}
		}
	}
	return dp[length1][length2];
}
int main()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		string str1, str2;
		cin >> str1 >> str2;
		cout << LCS(str1, str2) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
char map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	if(x < 0 || y < 0) return 0;
	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			return findMaxLength(x-1,y-1) +1;
		}else{
			return max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
	}else return map[x][y];
}

int main(){
	
	scanf("%d",&q);

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<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;
  C[0][0] = 0
  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]);
    }
  }javascript:void(0)

  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>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define nyan "(=^･ω･^=)"
using namespace std;

const int mod = 1e9 + 7;
const long long INF = 1LL<<60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//using ll = long long;
//using P = pair<int,int>;

int main(){
    int q;
    cin >> q;
    
    rep(i, q){
        string x, y;
        cin >> x >> y;
        
        int n = x.size();
        int m = y.size();
        
        // dp[i][j] : sのi番目, tのj番目までに対する 
        // longest common subsequenceの長さ
        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 (x[i] == y[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[n][m] << endl;
    }
}

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;
 int point;

  for(int i=0;i<n;i++){
    ans[i]=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]=0;
}
  }

 
 

  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<<ans[0]<<endl;
 cout<<ans[1]<<endl;
 cout<<ans[2]<<endl;

 return 0;
}

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

int main(){
    int N; cin >> N;
    for (int _ = 0; _ < N*2;_ += 2){
        string A,B;
        cin >> A >> B;
        
        vector<vector<int>> dp(A.size() +1 ,vector<int> (B.size() + 1,0));
        for (int i = 0;i < A.size();i ++){
            for(int j = 0; j < 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][j + 1],dp[i + 1][j]);
            }
        }
        cout << dp[A.size()][B.size()] << endl;
    }
}