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 FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

int dp[1020][1020];

int lcs(string x, string y) {
    int n=x.size(), m=y.size();
    rep(i,n) rep(j,m) {
        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() {
    int q; cin >> q;
    rep(data,q) {
        ZERO(dp);
        string x, y;
        cin >> x >> y;
        int ans = lcs(x,y);
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,memo[1111][1111];
  string x,y;
  memset(memo,0,sizeof(memo));
  cin>>n;
  for(int count=0;count<n;count++){
    cin>>x>>y;
    int h=x.size(),w=y.size();
    for(int i=0;i<=w;i++){
      for(int j=0;j<=h;j++){
	if(i==0||j==0) continue;
	if(x[j-1]==y[i-1]){
	  memo[i][j]=max(memo[i-1][j-1]+1,memo[i][j-1]);
	}else if(x[j-1]!=y[i-1]){
	  memo[i][j]=max(memo[i-1][j],memo[i][j-1]);
	}
      }
    }
    cout<<memo[w][h]<<endl;
  }
  return(0);
}

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 q;
  cin >> q;

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

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
const int INF_INT = 2147483647;
const long long INF_LONG = 9223372036854775807;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)


void f () {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size(), vector<int>(t.size()));
  tuple<int, int, int> tup(0, 0, 0);

  rep (i, t.size()) if(s[0] == t[i]) dp[0][i] = 1;
  rep (i, s.size()) if(t[0] == s[i]) dp[i][0] = 1;
  for (int i=1; i<s.size(); ++i) for (int j=1; j<t.size(); ++j) {
    if (s[i] == t[j]) dp[i][j] = 1;
    if (get<0>(tup) < dp[i][j]) tup = tie(dp[i][j], i, j);
  }

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

  cout << dp[s.size() - 1][t.size() - 1] << endl;
}


int main() {
  int q;  cin >> q;
  
  rep (i, q) f();
}

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();
  
  for (int i = 0; i < m; i++) c[i][0] = 0;
  for (int i = 0; i < n; i++) c[0][j] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) 
        if (i == 0 || j == 0) 
          c[i][j] = 1;
        else 
          c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

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 <stdio.h>
#include <vector>
#include <math.h>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        string s, t;
        cin >> s >> t;
        int dp[1001][1001] = {};
        for (int j = 0; j < s.length(); ++j) {
            for (int k = 0; k < t.length(); ++k) {
                if (s[j] == t[k]) {
                    dp[j + 1][k + 1] = max(dp[j + 1][k + 1], dp[j][k] + 1);
                }
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j+1][k]);
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j][k+1]);
            }
        }
        cout << dp[s.length()][t.length()] << endl;

    }
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int p[1001][1001]={0};
int main(){
int i,j,q,e,l,z,m,u,n;
char s[2001],c[2001];
cin>>q;
for(e=0;e<q;e++){
scanf("%s %s",s,c);
l=strlen(s);
z=strlen(c);

for(i=0;i<=l;i++){
	for(j=0;j<=z;j++){
		p[i][j]=0;
	}
}
for(i=1;i<=z;i++){
	for(j=1;j<=l;j++){
		if(c[i-1]==s[j-1])p[j][i]=p[j][i-1]+1;
		else p[j][i]=max(p[j][i-1],p[j][i-1]);
	}
}
cout<<p[l][z]<<endl;
}
return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+5;j++){
            for(int k = 0;k < Y.length()+5;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k-1] == X[j-1]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
    return 0;
}