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 <string>
using namespace std;
const int LEN = 1005;
string X,Y;
int dp[LEN][LEN];

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

		int xn = X.size();
		int yn = Y.size();

		for (int j = 0; j <= xn; ++j)
		{
			dp[j][0] = 0;
		}

		for (int j = 0; j <= yn; ++j)
		{
			dp[0][j] = 0;
		}

		for (int j = 0; j < xn; ++j)
		{
			for (int k = 0; k < yn; ++k)
			{
				if (X[j] == Y[k])
				{
					dp[j+1][k+1] = max(dp[j][k]+1, max(dp[j][k+1], dp[j+1][k]));
				}
				else
				{
					dp[j+1][k+1] = max(dp[j][k+1], dp[j+1][k]);
				}
			}
		}

		cout << dp[xn][yn] << endl;
	}

	
	return 0;
}

C++: #include<bits/stdc++.h> 
using namespace std;
using ll=long long;
const long long mod=1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int dp[1005][1005];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);    
int q=0;
cin>>q;
vector<vector<string>> s(q,vector<string>(2));
for(int i=0;i<q;i++)cin>>s.at(i).at(0)>>s.at(i).at(1);
for(int pos=0;pos<q;pos++){
    memset(dp,0,sizeof(dp));
    string a=s.at(pos).at(0),b=s.at(pos).at(1);
    for(int i=0;i<(int)a.size();i++){
        for(int j=0;j<(int)b.size();j++){
            if(a.at(i)==b.at(j))dp[i+1][j+1]=dp[i][j]+1;
            else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
        }
    }
    cout<<dp[a.size()][b.size()]<<endl;
}

}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int q;
  cin >> q;
  REP(i, q) {
    string X, Y;
    cin >> X >> Y;
    vector<vector<int>> dp(Y.size() + 5, vector<int>(X.size() + 5, 0));
    for (int i = 0; i < Y.size(); i++) {
      for (int j = 0; j < X.size(); j++) {
        if (Y[i] == X[j]) {
          chmax(dp[i + 1][j + 1], dp[i][j] + 1);
          chmax(dp[i + 1][j + 1], dp[i][j + 1]);
          chmax(dp[i + 1][j + 1], dp[i + 1][j]);
        } else {
          chmax(dp[i + 1][j + 1], dp[i][j + 1]);
          chmax(dp[i + 1][j + 1], dp[i + 1][j]);
        }
      }
    }
    cout << dp[Y.size()][X.size()] << endl;
  }
}

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=1;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<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
 
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A); 
strB = strlen(B);

    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
 
  return 0;
}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int n, m;
string s, t;
const int MAX = 1010;

int dp[MAX][MAX];

int rec(int si, int ti) {
    if (dp[si][ti] >= 0) {
        return dp[si][ti];
    }
    int res = 0;
    if (si == n and ti == m) {
        return res;
    }
    else {
        if (s[si] == t[ti]) {
            res = max(res, rec(si + 1, ti + 1) + 1);
        }
        else {
            if (si < n) {
                res = max(res, rec(si + 1, ti));
            }
            if (ti < m) {
                res = max(res, rec(si, ti + 1));
            }
        }
    }
    return (dp[si][ti] = res);
}

void solve() {
    int q;
    cin >> q;
    while (q--) {
        cin >> s >> t;
        n = s.size(), m = t.size();
        fill(dp[0], dp[MAX], -1);
        cout << rec(0, 0) << endl;
    }
}

int main() {
    solve();
}

C++: // longest common subsequence
#include<iostream>
#include<string>
#include<algorithm>
int LCS(std::string, std::string);
int N=1000;
int main(){
  int n;
  std::string str1, str2;
  std::cin >> n;
  for(int i=0; i<n; i++){
    std::cin >> str1 >> str2;
    std::cout << LCS(str1, str2) << std::endl;
  }
  return 0;
}
int LCS(std::string str1, std::string str2){
  int c[N+1][N+1], m=str1.size(), n=str2.size(), maxl=0;
  str1 = ' ' + str1, str2 = ' ' + str2;
  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(str1[i] == str2[j]) c[i][j] = c[i-1][j-1] + 1;
      else c[i][j] = std::max(c[i-1][j], c[i][j-1]);
      maxl = std::max(maxl, c[i][j]);
    }
  }
  return 0;
}