Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

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

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オリジン)
	int val;

	if(x < 0 || y < 0) return 0;

	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			val = findMaxLength(x-1,y-1) +1;
		}else{
			val = max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
		map[x][y] = val;//メモ
		return val;
	}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>

using namespace std;



int n;
string a, b;
string x[1000], y[1000];




int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        
        if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001][1001];
char x[1001], y[1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		scanf("%s%s", x, y);
		rep(i, 1000)rep(j, 1000) {
			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]);
		}
		printf("%d\n", dp[1000][1000]);
	}
}