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

using namespace std;

int lcs(const string & a, const string & b)
{
    vector<vector<int> > dp(a.size(), vector<int>(b.size()));

    for (size_t i = 0; i < dp.size(); ++i) {
        for (size_t j = 0; j < dp[0].size(); ++j) {
            if (a.at(i) == b.at(j)) {
                dp[i][j] = ((i == 0 || j == 0) ? 0 : dp[i - 1][j - 1]) + 1;
            } else {
                dp[i][j] = max(i == 0 ? 0 : dp[i - 1][j],
                               j == 0 ? 0 : dp[i][j - 1]);
            }
        }
    }

    return dp[a.length() - 1][b.length() - 1];
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        string a, b;
        cin >> a >> b;
        cout << lcs(a, b);
    }

    return 0;
}

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

#define MAX_NUM 100
int fLength;
int sLength;

int memo[MAX_NUM][MAX_NUM];
string f, s;

/*******************
  Longest Common Subsequence
  ??¨??????????????§??????????????????(??¨??????????????§?????????)
 *********************/

void solve() {
  for(int i = 0; i < fLength; i++) {
    for(int j = 0; j < sLength; j++) {
      if(f[i] == s[j]) {
        memo[i + 1][j + 1] = memo[i][j] + 1;
      }
      else {
        memo[i + 1][j + 1] = max(memo[i + 1][j], memo[i][j + 1]);
      }
    }  
  }
}


int main(void) {
  int n;
  cin >> n;


  //?????????
  memset(memo, 0, sizeof(memo));
  for(int i = 0; i < n; i++) {
    cin >> f >> s;
    fLength = f.size();
    sLength = s.size();
    solve();
    cout << memo[fLength][sLength] << endl;
  }
  return 0;
}

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

int main()
{
    int n;
    cin >> n;
    rep(k, n) {
        string x, y;
        cin >> x >> y;
        vector<vector<int>> dp(x.size(), vector<int>(y.size(), 0));
        int d;
        d = 0;
        for (int i = 0; i < x.size(); i++) {
            if (x[i] == y[0]) d = 1;
            dp[i][0] = d;
        }
        d = 0;
        for (int j = 0; j < y.size(); j++) {
            if (y[j] == x[0]) d = 1;
            dp[0][j] = d;
        }
        for (int i = 1; i < x.size(); i++) {
            for (int j = 1; j < y.size(); 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]);
                }
            }
        }
        cout << dp[x.size() - 1][y.size() - 1] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1001][1001];

int solve(string s1,string s2){
	int m=s1.size() ;
	int n=s2.size() ;
	s1=' '+s1;
	s2=' '+s2;
	memset(dp,0,sizeof(dp));
	int maxl=0;
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			if(s1[i]==s2[j])dp[i][j]=dp[i-1][j-1]+1;
			else {
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
			maxl=max(maxl,dp[i][j]);
		}
	}
	return maxl;
}

int main(){
	int t;
	cin>>t;
	while(t--){
		string s1,s2;
		cin>>s1>>s2;
		cout<<solve(s1,s2)<<endl;
	}
}

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

#define MAX 150
#define MAXL 1000

int C[MAXL+1][MAXL+1];

int lec(string X,string Y)
{
    int XL = X.length();
    int YL = Y.length();
    for(int i=0;i<=XL;i++)
        C[i][0] = 0;
    for(int i=0;i<=YL;i++)
        C[0][i] = 0;
    for(int i=0;i<=XL;i++)
    {
        for(int j=0;j<=YL;j++)
        {
            if(X[i+1]==Y[j+1])
            {
                C[i+1][j+1] = C[i][j]+1;
            }
            else if(C[i][j+1] > C [i+1][j])
            {
                C[i+1][j+1] = C[i][j+1];
            }else{
                C[i+1][j+1] = C[i+1][j];
            }
        }
    }
    return C[XL][YL];
}

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

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

int lcs(string X, string Y){
  int x = X.size();
  int y = Y.size();

  int d[y+1][x+1];
  memset(d, 0, sizeof(d));

  for(int i = 1; i <= y; i++){
    char a = Y[i];

    for(int j = 1; j <= x; j++){
      char b = X[j];

      if(a == b){
        d[i][j] = max(d[i][j], d[i-1][j-1] + 1);
      }else{
        d[i][j] = max(d[i][j], max(d[i-1][j], d[i][j-1]));
      }
    }
  }

  return d[y][x];
}

int main(){
  int n;
  string x, y;
  cin >> n;

  for(int i = 0; i < n; i++){
    cin >> x;
    cin >> y;

    cout << lcs(x, y) << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <vector>
using namespace std;

int q;
string strs[310];
int memo[1010][1010];

void initMemo(int l1, int l2) {
    for (int i = 0; i <= l1; i++) {
        memo[0][i] = 0;
    }

    for (int i = 0; i <= l2; i++) {
        memo[i][0] = 0;
    }
}

void store() {
    cin >> q;
    for (int i = 0; i < 2 * q; i++) {
        cin >> strs[i];
    }
}

// Longest Common Subsequence
void LCS(string s1, string s2) {
    int l1 = s1.length();
    int l2 = s2.length();
    initMemo(l1, l2);

    for (int i = 1; i <= l1; i++) {
        for (int j = 1; j <= l2; j++) {
            if (s1[i - 1] == s2[j - 1]) {
                memo[i][j] = memo[i - 1][j - 1] + 1;
            } else {
                memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
            }            
        }
    }

    cout << memo[l1][l2] << endl;
}

void AllLCS() {
    for (int i = 0; i < 2 * q; i += 2) {
        LCS(strs[i], strs[i + 1]);
    }
}


int main() {
    store();
    AllLCS();

    return 0;
}