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 <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 28;
const ll INF64 = 1ll << 55;
const int mod = 1000000007;
//const int mod = 998244353;

template <class T>inline bool chmin(T &a, const T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline bool chmax(T &a, const T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint &operator++()
  {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--()
  {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};

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

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

int dp[1010][1010];

int LCS(string s1, string s2)
{
	s1 = ' ' + s1;
	s2 = ' ' + s2;
	int maxl = 0;
	for(int i = 1; i <= s1.length(); ++ i)
	{
		for(int j = 1; j <= s2.length(); ++ 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 dp[s1.length()-1][s2.length()-1];
}

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

C++: leng :: Int
leng = 1001

getPos :: Int -> Int -> Int
getPos y x = y * leng + x

lcs :: String -> String -> Int -> Int
lcs in1 in2 = (map lcs_func [0 ..] !!)
    where
      lcs_func n
          | (length in1) == 1 || (length in2) == 1 = if (head in1) == (head in2) then 1 else 0
          | (head in1) == (head in2) = (lcs (tail in1) (tail in2) (getUpLeft n)) + 1
          | otherwise = max (lcs (tail in1) in2 (getUp n)) (lcs in1 (tail in2) (getLeft n))
      getUp n = n - leng
      getLeft n = n - 1
      getUpLeft n = n - leng - 1

solve :: String -> String -> Int
solve in1 in2 = lcs in1 in2 (getPos ((length in1) - 1) ((length in2) - 1))

input :: Int -> IO ()
input n
    | n == 0 = return ()
    | otherwise = do
  str1 <- getLine
  str2 <- getLine
  putStrLn $ show $ solve str1 str2
  input $ n-1
                         

main :: IO()
main = do
  n <- readLn
  input n

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define inf 1e9+7

int dp[1001][1001];
char x[1001],y[1001];

void init(){
    for(int i=0;i<1001;i++){
        dp[0][i]=0;
        dp[i][0]=0;
    }
}

void solve(int sn, int tn){
    for(int i=1;i<=sn;i++){
        for(int j=1;j<=tn;j++){
            if(x[i-1]==y[j-1])dp[i][j]=dp[i-1][j-1]+1;
            else dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    string s,t;
    for(int i=0;i<n;i++){
        init();
        cin >> s >> t;
        int sn=s.size();
        int tn=t.size();
        for(int j=0;j<sn;j++){
            x[j]=s[j];
        }
        for(int j=0;j<tn;j++){
            y[j]=t[j];
        }

        solve(sn, tn);
        cout << dp[sn][tn] << endl;
    }

}

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

constexpr int MAX = 1000;
int dp[MAX+1][MAX+1];

int main() {
  int N;
  cin >> N;
  for (int n{}; n < N; ++n) {
    string s, t;
    cin >> s >> t;
    for (size_t i{}; i < s.length(); ++i) {
      for (size_t j{}; j < t.length(); ++j) {
        if (s[i] == t[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]);
      }
    }
    cout << dp[s.length()][t.length()] << endl;
  }
  return 0;
}