Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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 <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
    return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

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

    auto solve = [](const string& s, const string& t) {
        const int n = s.length(), m = t.length();
        string s1 = " " + s, t1 = " " + t;

        auto dp = make_v(n + 1, m + 1, 0);
        
        FOR(i, 1, n + 1)FOR(j, 1, m + 1) {
            if (s1[i] == t1[j]) {
                dp[i][j] = max(dp[i][j], dp[i - 1][j -  1] + 1);
            }
            dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            dp[i][j] = max(dp[i][j], dp[i - 1][j]);
        }

        return dp[n][m];
    };

    REP(i, n) {
        string s, t; cin >> s >> t;

        cout << solve(s, t) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>

using namespace std;
static const int N = 150;
static const int AX = 1000, AY = 1000;
string  X, Y;

int do_dp(int ax,int ay) {

    int dp[AX][AY];



    for (int i = 0; i < ax; i++) {
        if (X[i] == Y[0]) dp[i][0] = 1;
        if (i > 0) dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }

    for (int j = 0; j < ay; j++) {
        if (X[0] == Y[j]) dp[0][j] = 1;
        if (j > 0) dp[0][j] = max(dp[0][j], dp[0][j - 1]);
    }

    for (int i = 1; i < ax; i++) {
        for (int j = 1; j < ay; 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]);
            }
        }
    }

    /*for(int i = 0;i < ax;i++){
    for(int j = 0;j < ay;j++){
    cout << dp[i][j] << " ";
    }
    cout << '\n';
    }*/

    return dp[ax - 1][ay - 1];
}

int main() {
    string str;
    int q;

    cin >> q;

    int A[N];

    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        //cout << X.size() << " " << Y.size() << '\n';
        A[i] = do_dp(X.size(),Y.size());
    }

    for (int i = 0; i < q; i++) {
        cout << A[i] << '\n';
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

string LCS(string s, string t){
    ll ns = s.size();
    ll nt = t.size();
    vector< vector<ll> > dp(ns+1, vector<ll> (nt+1, 0));
    // initilaize
    rep(i, 0, ns){
        if(s[i] == t[0]) dp[i][0] = 1;
    }
    rep(i, 0, nt){
        if(s[0] == t[i]) dp[0][i] = 1;
    }
    // build DP table
    rep(i, 1, ns){
        rep(j, 1, nt){
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            if(s[i] == t[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }
        }
    }
    // make LCS
    string ret;
    ll i = ns-1, j = nt-1;
    while(i >= 0 && j >= 0){
        if(s[i] == t[j]){
            ret.push_back(s[i]);
            i--; j--;
        }else{
            if(j == 0) i--;
            else if(i == 0) j--;
            else if(dp[i-1][j] > dp[i][j-1]) i--;
            else j--;
        }
    }
    return ret;
}

int main(){
    ll Q;
    cin >> Q;
    rep(q, 0, Q){
        string x, y;
        cin >> x >> y;
        cout << LCS(x, y).size() << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <memory>
#include <string>
#include <map>
using namespace std;
#define MAX 1003

char A[MAX];
char B[MAX];

void print(){}
int DP[MAX][MAX];
int dp(int i,int j){
    if(i==0||j==0) return 0;
    if(DP[i][j]!=-1) return DP[i][j];
    if(A[i]==B[j]) return DP[i][j]=dp(i-1,j-1)+1;
    return DP[i][j]=max(dp(i,j-1),dp(i-1,j));
}
void solve(){
    memset(DP,-1,sizeof(DP));

    int tmp;
        scanf("%d",&tmp);
        while(tmp--){
                memset(DP,-1,sizeof(DP));
                scanf("%s%s",A,B);
            printf("%d\n",dp(strlen(A),strlen(B)));
        }

}

int main(){

  //  freopen("in","r",stdin);
    solve();
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
 
#define N 1000
 
char A[N+1],B[N+1];
  

  
int Max(int a,int b){
  if(a>=b) return a;
  else return b;
}
int main(){
  int k,a,i,j,m,n,max,c[N+1][N+1];
  scanf("%d",&a);
  for(k=0;k<a;k++){
    scanf("%s",A);
    scanf("%s",B);
    max=0;
     m=strlen(A);
  n=strlen(B);
  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(A[i-1]==B[j-1]) c[i][j]=c[i-1][j-1] + 1;
      else if(c[i-1][j]>=c[i][j-1]) c[i][j]=c[i-1][j];
      else c[i][j]=c[i][j-1];
      max=Max(max,c[i][j]);
    }
  }
    printf("%d\n",max);
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int N = 1000;
char s[N+1], t[N+1];
int ls, lt;
int q;
int dp[N+1][N+1];
//dp[i][j] = s[0]...s[i+1]??¨t[0]...t[j+1]???LCS
//(1<=i<=ls, 1<=j<=lt)

int main() {
  int i, j, k;
  scanf("%d ", &q);
  for (k=0; k<q; k++) {
    ls = lt = 0;
    memset(s, 0, sizeof(s)), scanf("%s ", s);
    memset(t, 0, sizeof(t)), scanf("%s ", t);
    while (s[ls]) ls++;
    while (t[lt]) lt++;

    memset(dp, 0, sizeof(dp));
    for (i=1; i<=ls; i++)
      for (j=1; j<=lt; j++) {
        if (s[i]==t[j])
          dp[i][j] = dp[i-1][j-1] + 1;
        else
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      }

    printf("%d\n", dp[ls][lt]);
  }

  return 0;
}