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 <vector>
#include <string>
#include <algorithm>

int lcs(std::string str1, std::string str2) {
    std::vector<std::vector<int> > table(str1.size(), std::vector<int>(str2.size(), 0));
    // Initialization
    for (int i=0; i<str1.size(); i++) {
        if ( str1[i] == str2[0] ) {
            table[i][0] = 1;
        } else {
            table[i][0] = 0;
        }
    }
    for (int j=0; j<str2.size(); j++) {
        if ( str2[j] == str1[0] ) {
            table[0][j] = 1;
        } else {
            table[0][j] = 0;
        }
    }
    // Calculate LCS
    for (int i=1; i<str1.size(); i++) {
        for (int j=1; j<str2.size(); j++) {
            table[i][j] = std::max( table[i-1][j], table[i][j-1] );
            if ( str1[i] == str2[j] ) {
                table[i][j]++;
            }
        }
    }
    return table[str1.size()-1][str2.size()-1];
}

int main() {
    int n;
    std::cin >> n;
    std::string str1, str2;
    std::vector<int> res(n);
    for (int i=0; i<n; i++) {
        std::cin >> str1 >> str2;
        res[i] = lcs(str1, str2); 
    }
    for (int i=0; i<n; i++) {
        std::cout << res[i] << std::endl;
    }
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1010][1010];

void solve(string x, string y)
{
	for (int i = 0; i != x.size(); i++)
	{
		for (int j = 0; j != y.size(); j++)
		{
			if (x[i] == y[j])
			{
				LCS[i + 1][j + 1] = LCS[i][j] + 1;
			}
			else
			{
				LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

int main()
{
	int q;
	cin >> q;

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

	for (int i = 0; i < q; i++)
	{
		string x, y;
		cin >> x >> y;
		solve(x, y);
	}
	return 0;
}

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

int	lcs(const string &X, const string &Y)
{
	int	len_X = X.size(), len_Y = Y.size();
	int	len_lcs[1001][1001] = {};
	for (int i = 0; i < len_X; ++i) {
		for (int j = 0; j < len_Y; ++j) {
			int	prev_X = len_lcs[i][j + 1];
			int	prev_Y = len_lcs[i + 1][j];
			int	prev = max(prev_X, prev_Y);	
			if (X.at(i) == Y.at(j)) {
				len_lcs[i + 1][j + 1] = max(len_lcs[i][j] + 1, prev);
			} else {
				len_lcs[i + 1][j + 1] = prev;
			}
		}
	}
	return len_lcs[len_X][len_Y];
}

int	main()
{
	int	q;
	cin >> q;
	string	X, Y;
	for (int i = 0; i < q; ++i) {
		cin >> X >> Y;
		cout << lcs(X, Y) << endl;
	}
}

C++: #include<iostream>
using namespace std;
/*int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};*/
int main()
{
    int num;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        int lcs[1001][1001]={};
        string a,b;
        cin>>a>>b;
        //cout<<lcs(a,b)<<endl;
        for(int i=0;i<a.length()+1;i++)
        {
            for(int j=0;j<b.length()+1;j++)
            {
                if(i==0||j==0)
                {
                    lcs[i][j]=0;
                }
                else if(a[i]==b[j])
                {
                    lcs[i][j]=lcs[i-1][j-1]+1;
                }
                else
                {
                    lcs[i][j]=max(lcs[i][j-1],lcs[i-1][j]);
                }
            }
        }
        cout<<lcs[a.length()][b.length()]<<endl;
    }
}

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

int dp[1002][1002] = { 0 };

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		string x, y;
		int cnt = 0;
		cin >> x >> y;
		memset(dp, 0, sizeof(dp));

		for (int j = 1; j <= x.size(); ++j) {
			for (int j2 = 1; j2 <= y.size(); ++j2) {
				if (x[j - 1] == y[j2 - 1]) {
					dp[j][j2] = max(dp[j][j2 - 1], dp[j - 1][j2 - 1] + 1);
				}
				else {
					dp[j][j2] = max(dp[j][j2 - 1], dp[j - 1][j2]);
				}
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}

	return 0;
}

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

int main(){
        int q;
        scanf("%d",&q);
        while(q--){
                string str1,str2;
                cin >> str1 >> str2;
                vector<vector<int>> dp(str1.size()+1,vector<int>(str2.size()+1,0));
                for(int i = 0; i < str1.size(); i++){
                        for(int j = 0; j < str2.size(); j++){
                                if(str1[i] == str2[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[str1.size()][str2.size()] << endl;
        }
        return 0;
}