Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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

int main() {
  int n, max_val;;
  cin >> n;
  string first;
  string second;
  for (int k = 0; k < n; k++) {
    cin >> first;
    cin >> second;
    int result[first.size()+1][second.size()+1];
    max_val = 0;
    for (int i = 0; i <= first.size(); i++) result[i][0] = 0;
    for (int i = 0; i <= second.size(); i++) result[0][i] = 0;
    for (int i = 1; i <= first.size(); i++) {
      for (int j = 1; j <= second.size(); j++) {
        int col = first.size();
        if (first[i-1] == second[j-1]) {
          result[i][j] = result[i-1][j-1] + 1;
        } else {
          result[i][j] = max(result[i-1][j],
                             result[i][j-1]);
        }
        max_val = max(result[i][j], max_val);
      }
    }
    cout << max_val << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

int q;
int dp[1000][1000];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( ret, std::max( rec( i+1, j ), rec( i, j+1 ) ) );

  return ret;
}

int main()
{
  std::cin >> q;
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

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

using namespace std;

string X, Y;
int M[1001][1001];

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> X >> Y;
		for (int i = 0; i <= X.length(); i++) {
			for (int j = 0; j <= Y.length(); j++) {
				M[i][j] = 0;
			}
		}
		for (int i = 0; i < X.length(); i++) {
			for (int j = 0; j < Y.length(); j++) {
				if (X[i] == Y[j]) M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i][j] + 1);
				M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i + 1][j]);
				M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i][j + 1]);
			}
		}
		cout << M[X.length()][Y.length()] << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include <string.h>
#include <algorithm>

using namespace std;

int main(){
  int N;cin>>N;
  string X[N], Y[N];
  for (int i=0;i < N; i++) {
    cin>>X[i];cin>>Y[i];
  }
  for (int i=0;i < N; i++) {
    string x_t=X[i];int x_len=x_t.length();
    string y_t=Y[i];int y_len=y_t.length();
    int dp[x_len+1][y_len+1];
    memset(dp,0,sizeof(dp));
    for (int x = 0; x <= x_len; x++)dp[x][0]=0;
    for (int y = 0; y <= y_len ; y++)dp[0][y]=0;
    for (int x=1;x<=x_len;x++){
      for (int y=1;y<=y_len;y++){
        if(x_t[x-1]==y_t[y-1]){
          dp[x][y]=dp[x-1][y-1]+1;
        }else{
          dp[x][y]=max(dp[x][y-1],dp[x-1][y]);
        }
      }
    }
    cout<< dp[x_len][y_len] << "\n";
  }

}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
  
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
  
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A);
strB = strlen(B);
 
    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>

using namespace std;

#define ARRAY_MAX 150
#define MAX 1000 + 1

char X[MAX];
char Y[MAX];

#pragma region 暴力递归
int FindLCS1(char x[], int i, char y[], int j)
{
	int l1 = strlen(x);
	int l2 = strlen(y);

	// 到索引末尾结束
	if (i >= l1 || j >= l2) return 0;

	if (x[i] == y[j])
		return 1 + FindLCS1(x, i + 1, y, j + 1);
	else
		return max(FindLCS1(x, i, y, j + 1), FindLCS1(x, i + 1, y, j));
}

int LongestCommonSubsequence1(char x[], char y[])
{
	return FindLCS1(x, 0, y, 0);
}
#pragma endregion

#pragma region 记忆搜索
int memory[MAX][MAX];
int FindLCS2(char x[], int i, char y[], int j)
{
	int l1 = strlen(x);
	int l2 = strlen(y);

	// 到索引末尾结束
	if (i >= l1 || j >= l2) return 0;

	if (memory[i][j] != -1) return memory[i][j];

	if (x[i] == y[j])
		return memory[i][j] = 1 + FindLCS2(x, i + 1, y, j + 1);
	else
		return memory[i][j] = max(FindLCS2(x, i, y, j + 1), FindLCS2(x, i + 1, y, j));
}

int LongestCommonSubsequence2(char x[], char y[])
{
	int l1 = strlen(x);
	int l2 = strlen(y);
	for (int i = 0; i < l1; ++i)
		for (int j = 0; j < l2; ++j)
			memory[i][j] = -1;

	return FindLCS2(x, 0, y, 0);
}
#pragma endregion

#pragma region 动态规划
int dp[MAX][MAX];

int LongestCommonSubsequence3(char x[], char y[])
{
	int l1 = strlen(x);
	int l2 = strlen(y);
	int size = sizeof(int) * (MAX) * (MAX);
	memset(dp, 0, size);
	for (int i = l1 - 1; i >= 0; --i)
		for (int j = l2 - 1; j >= 0; --j)
		{
			if (x[i] == y[j])
				dp[i][j] = 1 + dp[i + 1][j + 1];
			else
				dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
		}

	return dp[0][0];
}
#pragma endregion

int main()
{
	int q; cin >> q;
	queue<int> result;
	for (int i = 0; i < q; ++i)
	{
		cin >> X;
		cin >> Y;
		result.push(LongestCommonSubsequence3(X, Y));
	}

	while (!result.empty())
	{
		cout << result.front() << endl;
		result.pop();
	}

	return 0;
}