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>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <climits>
 
using namespace std;
 
int f(map<string,int>& dmap,const string& dstr1,string dstr2);
 
int main()
{
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        string data[2];
        map<string,int> dmap;
        cin>>data[0]>>data[1];
        if(data[0].length()<data[1].length()){
            cout<<f(dmap,data[1],data[0])<<endl;
        }else{
            cout<<f(dmap,data[0],data[1])<<endl;
        }
    }
}
int f(map<string,int>& dmap,const string& dstr1,string dstr2)
{
    auto dit=dmap.find(dstr2);
    if(dit!=dmap.end()){
        return (*dit).second;
    }
    int j=0;
    for(int i=0;i<dstr1.length()&&j<dstr2.length();i++,j++){
        while(i<dstr1.length()&&dstr1[i]!=dstr2[j]){
            i++;
        }
    }
    if(j==dstr2.length()){
        dmap.insert(pair<string,int>(dstr2,(int)dstr2.length()));
        return dstr2.length();
    }
    if(dstr2.length()==1){
        dmap.insert(pair<string,int>(dstr2,0));
        return 0;
    }
    f(dmap,dstr1,dstr2.substr(1,dstr2.length()-1));
    f(dmap,dstr1,dstr2.substr(0,dstr2.length()-1));
    j=0;
    for(int i=2;i<dstr2.length();i++){
        int c=f(dmap,dstr1,dstr2.substr(0,i-1)+dstr2.substr(i,dstr2.length()-i));
        if(j<c){
            j=c;
        }
    }
    dmap.insert(pair<string,int>(dstr2,j));
    return j;
}

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

int main(){
    int q;
    cin >> q;
    for(int qnum = 0; qnum < q; qnum++){
        string x, y;
        cin >> x;
        cin >> y;
        vector<vector<int>> memo(x.length()+1, vector<int>(y.length()+1, 0));
        for(int i = 0; i < x.length(); i++){
            for(int j = 0; j < y.length(); j++){
                if(x[i] == y[j]){
                    memo[i+1][j+1] = memo[i][j]+1;
                }else{
                    memo[i+1][j+1] = max(memo[i][j+1], memo[i+1][j]);
                }
            }
        }
        cout << memo[x.length()][y.length()] << endl;
    }
    return 0;
}

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

int main()
{
    int q;
    cin >> q;
    while (q > 0)
    {
        int dp[1010][1010] = {0};

        string X, Y;
        cin >> X >> Y;

        for (int i = 0; i <= X.size(); i++)
        {
            for (int j = 0; j <= Y.size(); j++)
            {
                if (X[i] == Y[j])
                {
                    dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j] + 1);
                }
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
            }
        }

        cout << dp[X.size()][Y.size()] << endl;
        q--;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
int main(void){
    int cnt;
    int n;
    std::cin>>n;

    for(int k=0;k<n;k++){
        std::string str1;
        std::string str2;
        std::cin>>str1;
        std::cin>>str2;
        int l1=str1.size();
        int l2=str2.size();
        int A[l1][l2]={};

        for(int i=0;i<l1;i++){
            for(int j=0;j<l2;j++){
                if(i==0 or j==0){
                    if(str1[i]==str2[j]){
                        A[i][j]=1;
                    }else if(i==0){
                        if(A[i][j-1]==1){
                            A[i][j]=1;
                        }
                    }else if(j==0){
                        if(A[i-1][j]==1){
                            A[i][j]=1;
                        }
                    }
                }else if(str1[i]==str2[j]){
                    A[i][j]=A[i-1][j-1]+1;
                }else{
                    A[i][j]=std::max(A[i][j-1],A[i-1][j]);
                }
                //std::cout<<A[i][j]<<" ";
            }
            //std::cout<<"\n";
        }
        std::cout<<A[l1-1][l2-1]<<"\n";
    }
    return 0;
}

C++: import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String X = null, Y = null;
        int[][] dp = new int[1001][1001];

        int q = sc.nextInt();

        for (int i = 0; i < q; ++i) {
            X = sc.next();
            Y = sc.next();
            for (int x = 0; x < X.length(); ++x) {
                for (int y = 0; y < Y.length(); ++y) {
                    if (X.charAt(x) == Y.charAt(y)) {
                        dp[x + 1][y + 1] = dp[x][y] + 1;
                    }
                    else {
                        if (dp[x + 1][y] > dp[x][y + 1]) {
                            dp[x + 1][y + 1] = dp[x + 1][y];
                        }
                        else {
                            dp[x + 1][y + 1] = dp[x][y + 1];
                        }
                    }
                }
            }
            System.out.printf("%d\n", dp[X.length()][Y.length()]);
        }
    }
}

C++: #include <iostream>

using namespace std;

static const int N = 1000;

int lcs(string x, string y)
{
    int dp[N + 1][N + 1];
    int m = x.length();
    int n = y.length();
    x = ' ' + x;
    y = ' ' + y;

    for (int i = 1; i <= m; i++)
        dp[i][0] = 0;
    for (int i = 1; i <= n; i++)
        dp[0][i] = 0;

    int res = 0;
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (x[i] == y[j])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }

    return dp[m][n];
}

int main()
{
    int n;
    cin >> n;
    string x1, x2;
    for (int i = 0; i < n; i++)
    {
        cin >> x1 >> x2;
        cout << lcs(x1, x2) << endl;
    }
    return 0;
}