Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	lenX, lenY := len(strX), len(strY)
	memo := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		memo[i] = make([]int, lenY+1)
	}

	lcs := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if strX[i-1] == strY[j-1] {
				memo[i][j] = memo[i-1][j-1] + 1
			} else {
				if memo[i-1][j] > memo[i][j-1] {
					memo[i][j] = memo[i-1][j]
				} else {
					memo[i][j] = memo[i][j-1]
				}
			}
			if memo[i][j] > lcs {
				lcs = memo[i][j]
			}
		}
	}
	return lcs
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;

void solve(string s1, string s2) {
    vector<vector<int> > dp(s1.size()+1, vector<int>(s2.size()+1,0));
    rep(i, s1.size()+1) {
        rep(j, s2.size()+1) {
            if(i == 0 || j == 0)
                continue;
            if(s1[i-1] == s2[j-1]){
                dp[i][j] = dp[i-1][j-1]+1;
            }
            else {
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            }
        }
    }
    cout << dp[s1.size()][s2.size()] << endl;
    return;
}

int main(){
    int n; cin >> n;
    string s1,s2;
    rep(i, n) {
        cin >> s1 >> s2;
        solve(s1,s2);
    }
}

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

static const int N = 1000;

int lcs(string X,string Y){
  int c[N+1][N+1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;

  X = ' ' + X;
  Y = ' ' + Y;

  for(int i=0;i<=m;i++){
    c[i][0]=0;
  }
  for(int j=1;j<=n;j++){
    c[0][j]=0;
  }

  for(int i=1;i<=m;i++){
    for(int 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;
}

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

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

int q;
string X,Y;
int L[1001][1001];

int max(int i,int j) {return i > j ? i : j;}

int getL(int i,int j) {
	if(L[i][j] != -1) return L[i][j];

	if(i == 0 || j == 0) return 0;
	if(X[i - 1] == Y[j - 1]) return 1 + getL(i - 1,j - 1);
	return max(getL(i,j - 1),getL(i - 1,j));
}

int main() {

	cin >> q;
	for(int i = 0;i < q;i++) {
		cin >> X >> Y;
		for(int j = 0;j <= X.size();j++) {
			for(int k = 0;k <= Y.size();k++) L[j][k] = -1;
		}
		cout << getL(X.size(),Y.size()) << endl;
	}
}

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

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int N;
  cin>>N;
  string S1,S2;
  for (int k=0; k<N; k++) {
    cin>>S1>>S2;
    int m=S1.length();
    int n=S2.length();
    S1=' '+S1;
    S2=' '+S2;
    int lcs[1001][1001];
    for (int i=1; i<=m; i++) {
      lcs[i][0]=0;
    }
    for (int j=1; j<=n; j++) {
      lcs[0][j]=0;
    }
    int maxl=0;
    for (int i=1; i<=m; i++) {
      for (int j=1; j<=n; j++) {
        if (S1[i]==S2[j]) {
          lcs[i][j]=lcs[i-1][j-1]+1;
        }
        else {
          lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
        }
        maxl=max(maxl,lcs[i][j]);
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N =1000;

int lcs(string Xtw,string Ytw){
    int c[N+1][N+1];
    int m=Xtw.size();
    int n=Ytw.size();
    int maxl=0;
    Xtw=' '+Xtw;
    Ytw=' '+Ytw;
    for(int i=0;i<=m;i++)c[i][0]=0;
    for(int j=1;j<=n;j++)c[0][j]=0;

    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(Xtw[i]==Ytw[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;
}

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

C++: #include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    string x,y;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        int c[x.size()+1][y.size()+1];
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
                if(j==0||k==0)c[j][k]=0;
                else if(j>0&&k>0&&x[j-1]==y[k-1]) c[j][k]=c[j-1][k-1]+1;
                else if(j>0&&k>0&&x[j-1]!=y[k-1])c[j][k]=max(c[j][k-1],c[j-1][k]);
            }
        }
        int maxi=0;
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
               maxi=max(maxi,c[j][k]);
               c[j][k]=0;
            }
        }
        cout<<maxi<<endl;
    }
    
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define endl '\n'
using namespace std;
int dy[4] = { 1, 0, -1, 0 }, dx[4] = { 0, 1, 0, -1 };
typedef pair<ll, ll> P;
const int MAX_N = 1000;
int dp[MAX_N + 1][MAX_N + 1];
string x, y;
int q;

int solve(string x, string y)
{
    int n = x.size(), m = y.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            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]);
            }
        }
    }
    return dp[n][m];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        cout << solve(x, y) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int Max = 1000;
int getlong(string a, string b){
int n,m,i,j,as=0;
	int cs[Max + 1][Max + 1];
	n=a.size();
	m=b.size();

	a=' '+a;
	b=' '+b;
	cs[0][0]=0;
	for(i=1;i<=n;i++)
	cs[i][0]=0;
	for(i=1;i<=m;i++)
	cs[0][i]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			if(a[i]==b[j])
			cs[i][j]=cs[i-1][j-1]+1;
			else
			cs[i][j]=max(cs[i][j-1],cs[i-1][j]);
		as=max(as,cs[i][j]);
		}
		
	}
	return as;
}
main(){
	int q,i;
	string a,b;
	cin>>q;
	for(i=0;i<q;i++){
		cin>>a>>b;
		printf("%d\n",getlong(a,b));
	}
	return 0;
}