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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  ans[l]=N[X.size()][Y.size()];
  l++;
  // cout<<N[X.size()][Y.size()]<<endl;
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }
  cout<<ans[0]<<endl;
 cout<<ans[1]<<endl;
 cout<<ans[2]<<endl;
}

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

int preX[1020][30];
int dp[1020];

int main() {
	int q;
	cin >> q;
	for(int t = 0; t < q; t++) {
		string X, Y;
		for(int i = 0; i < 1020; i++) {
			for(int j = 0; j < 30; j++) {
				preX[i][j] = 0;
			}
			dp[i] = 0;
		}
		cin >> X >> Y;
		for(int i = 0; i < X.size(); i++) {
			for(int j = 0; j < 30; j++) {
				preX[i+1][j] = preX[i][j];
			}
			if(i > 0)
				preX[i+1][X[i-1]-'a'] = i;
		}
		for(int i = 0; i < Y.size(); i++) {
			for(int j = (int)X.size()-1; j >= 0; j--) {
				if(Y[i] == X[j])
					for(int k = 0; k < 30; k++)
						dp[j+1] = max(dp[j+1], dp[preX[j+1][k]] + 1);
			}
		}
		int res = 0;
		for(int i = 0; i < 1020; i++)
			res = max(res, dp[i]);
		cout << res << endl;
	}
}

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

using namespace std;

#define ll long long
typedef complex<double> P;

const int MAX_SIZE = 1001;
int q;
int dp[MAX_SIZE][MAX_SIZE] = {};

int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {

    for (int j = 0; j < MAX_SIZE; j++) {
      dp[MAX_SIZE-1][0] = 0;
      dp[0][MAX_SIZE-1] = 0;
    }

    string a, b; cin >> a >> b;

    for (int j = 0; j < a.size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        if(a[j] == b[k]) {
          dp[j+1][k+1] = dp[j][k] + 1;
        }
        else {
          dp[j+1][k+1] = max(dp[j][k+1], dp[j+1][k]);
        }
      }
    }

    cout << dp[a.size()][b.size()] << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<iomanip>
#include<limits>
#include<deque>
#include<map>
#include<list>
#include<set>
#include <unordered_set>
#include<vector>
#include<cmath>
#include<cstdio>
#include<memory>
#include<bitset>
#include<stack>
#include<functional>
#include<queue>
#include<regex>
#include<time.h>

using namespace std;

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl

#define Tof(x) (x) ? Yes : No
#define TOF(x) (x) ? YES : NO
#define tof(x) (x) ? yes : no

using ll = long long;

constexpr ll MOD = 1000000007;
constexpr ll INF = 1050000000;



/*for文でx,yを上下左右確認するやつだよ。正直使いづらいよ。

int dx[] = { 1,-1, 1,-1,0,1,-1, 0 };
int dy[] = { 1,-1,-1, 1,1,0, 0,-1 };


for (int i = 0; i < 4; i++) {
	int ny = y + dy[i];
	int nx = x + dx[i];
}

*/

/*繰り返し二乗法だよ。modもとってくれるよ。n^kをmodで割った余りでやってくれるよ。

ll POW_MOD(ll n, ll k, ll mod) {
	ll r = 1;

	for (; k > 0; k >>= 1) {
		if (k & 1) {
			r = (r * n) % mod;
		}
		n = (n * n) % mod;
	}
	return r;
}

*/

/*2重for文書くのめんどくなった時用だよ。

for (int i = 0; i < h; ++i) {
	for (int j = 0; j < w; ++j) {

	}
}

*/

struct node {
	int value;
	node* next;
	node(int value, node* next) : value(value), next(next) { }
};

#define index_of(as, x) distance(as.begin(), lower_bound(as.begin(), as.end(), x))

vector<int> lcs_hs(const vector<int>& a, const vector<int>& b) {
	const int n = a.size(), m = b.size();
	map< int, vector<int> > M;
	for (int j = m - 1; j >= 0; --j)
		M[b[j]].push_back(j);
	vector<int> xs(n + 1, INF); xs[0] = -INF;
	vector< node* > link(n + 1);
	for (int i = 0; i < n; ++i) {
		if (M.count(a[i])) {
			vector<int> ys = M[a[i]];
			for (int j = 0; j < ys.size(); ++j) {
				int k = index_of(xs, ys[j]);
				xs[k] = ys[j];
				link[k] = new node(b[ys[j]], link[k - 1]);
			}
		}
	}
	int l = index_of(xs, INF - 1) - 1;
	vector<int> c;
	for (node* p = link[l]; p; p = p->next)
		c.push_back(p->value);
	reverse(c.begin(), c.end());
	return c;
}


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

	for (int i = 0; i < n; i++) {
		vector<int> a, b;
		string s; cin >> s;
		for (int j = 0; j<s.size(); j++) {
			char x = s[j];
			a.push_back((int)x);
		}
		string t; cin >> t;
		for (int j = 0; j<t.size(); j++) {
			char y = t[j];
			b.push_back((int)y);
		}
		vector<int> c = lcs_hs(a, b);
		cout << c.size() << endl;
	}


	return 0;
}

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



int lcs(string x, string y){

  int m = x.length();
  int n = y.length();

  int c[m + 1][n + 1];

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

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

  }

  return c[m][n];
}

int main(){

  int q;

  string x, y;

  cin >> q;

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



  return 0;
}