Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for 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])
		}
	}
	return maxl
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 1005;
char inputa[MAXN], inputb[MAXN];
int f[MAXN][MAXN];
int main(void)
{
    int t, lengtha, lengthb;
    scanf("%d", &t);

    while(t--)
    {
        scanf("%s%s", inputa, inputb);
        memset(f, 0, sizeof(f));

        lengtha = strlen(inputa);
        lengthb = strlen(inputb);

        for(int i = 0; i <= lengtha; i++)
        {
            for(int j = 0; j <= lengthb; j++)
            {
                if(i == 0 || j == 0)
                    f[i][j] = 0;
                else if(inputa[i - 1] == inputb[j - 1])
                    f[i][j] = f[i - 1][j - 1] + 1;
                else
                    f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            }
        }

        printf("%d\n", f[lengtha][lengthb]);
    }

    return 0;
}

C++: // C++ 14
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>

#define ll long long
#define loop(x, start, end) for(int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)

#define MAX 1000

using namespace std;



int lcs(string &s1, string &s2) {
    int dp[MAX+1][MAX+1];
    int l1 = s1.length();
    int l2 = s2.length();
    int maxl = 0;
    
    s1 = ' ' + s1;
    s2 = ' ' + s2;
    
    loop(i, 0, l1+1) {
        dp[i][0] = 0;
    }
    loop(j, 0, l2+1) {
        dp[0][j] = 0;
    }
    
    loop (i, 1, l1+1) {
        loop (j, 1, l2+1) {
            if (s1[i] == s2[j]) dp[i][j] = 1 + dp[i-1][j-1];
            else dp[i][j] = max(
                dp[i-1][j],
                dp[i][j-1]
            );
            
            maxl = max(maxl, dp[i][j]);
        }
        
    }
    
    return maxl;
}

int main(void) {
    int n;
    string s1, s2;
    
    cin >> n;
    loop(i, 0, n) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,j) for (int (i)=0;(i)<(int)(j);++(i))
#define put(i) cout << i << endl
using namespace std;
const int MAX = 1000;

int n;
string s1, s2;
int dp[MAX+1][MAX+1];

int rec(int x, int y) {
    if (dp[y][x] >= 0) {
        return dp[y][x];
    }
    int sz1 = s1.size(), sz2 = s2.size();
    int ret;
    if (x == sz1 || y == sz2) {
        ret = 0;
    }
    else if (s1[x] == s2[y]) {
        ret = rec(x+1, y+1)+1;
    }
    else {
        ret = max(rec(x+1, y), rec(x, y+1));
    }
    return dp[y][x] = ret;
}

void solve() {
    while (n--) {
        memset(dp, -1, sizeof(dp));
        cin >> s1 >> s2;
        cout << rec(0, 0) << endl;
    }
}

int main() {
    cin >> n;
    solve();
}

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