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;
int main(){
    int q;
    cin >> q;
    string s1, s2;
    for(int i = 0; i < q; i++){
        cin >> s1 >> s2;
        int l1 = s1.size() + 1, l2 = s2.size() + 1, maxl = 0;
        int dp[1000 + 1][1000 + 1];
        for(int j = 0; j < l1; j++) dp[j][0] = 0;
        for(int k = 0; k < l2; k++) dp[0][k] = 0;
        for(int j = 1; j < l1; j++){
            for(int k = 1; k < l2; k++){
                if(s1[j - 1] == s2[k - 1]) dp[j][k] = dp[j - 1][k - 1] + 1;
                else dp[j][k] = max(dp[j - 1][k], dp[j][k - 1]);
                maxl = max(maxl, dp[j][k]);
            }
        }
        cout << maxl << endl;
    }
}

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

int cal(string na,string me){
  int l=na.size();
  int r=me.size();
  int h[1005][1005];
  for(int i=0;i<l;i++){
    for(int j=0;j<r;j++){
      h[i][j]=0;
    }
  }

  int ans=0;
  for(int i=1;i<=l;i++){
    for(int j=1;j<=r;j++){

      if(na[i-1]==me[j-1]) h[i][j]=h[i-1][j-1]+1;
      else h[i][j]=max(h[i][j-1],h[i-1][j]);

      ans=max(ans,h[i][j]);
    }
  }
  return ans;
}

int main(){
  int n,ans=0;
  cin>>n;

  string na,me;
  for(int i=0;i<n;i++){
    cin>>na>>me;
    ans=cal(na,me);
    cout<<ans<<endl;
  }

  return 0;
}

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


using namespace std;
const int MaxStringSize = 1000;
const int NullMemo = -1;

int calc_LCS(string str1, string str2) {
  // Memo[n][m] は str1.sub(0, n) と str2.sub(0, m)のLCSをmemoする。
  int memoLCS[str1.size() + 1][str2.size() + 1];

  for(int i = 0; i <= str1.size(); i++) {
    memoLCS[i][0] = 0;
  }
  for(int i = 0; i <= str2.size(); i++) {
    memoLCS[0][i] = 0;
  }

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

  return memoLCS[str1.size()][str2.size()];
}

int main() {
  int datasets_num;
  string str1;
  string str2;

  cin >> datasets_num;

  for(int i = 0; i < datasets_num; i++) {
    cin >> str1 >> str2;

    cout << calc_LCS(str1, str2) << endl;
  }
}

C++: #include<iostream>
#include<cstring>
#define LcsMaxLen 1005
using namespace std;
char sf[LcsMaxLen];
char ss[LcsMaxLen];
int mark[LcsMaxLen][LcsMaxLen]={0};
int LcsLen( int sfi,int ssi ){
	if(sfi<0||ssi<0)
		return 0;
	if(mark[sfi][ssi])return mark[sfi][ssi];
	if(sf[sfi]==ss[ssi]){
		mark[sfi][ssi] = LcsLen(sfi-1,ssi-1)+1;
		return mark[sfi][ssi];
	}
	else {
		mark[sfi][ssi] = max(LcsLen(sfi,ssi-1),LcsLen(sfi-1,ssi));
		return mark[sfi][ssi];
	}
} 
int main(){
	int n,len;
	cin>>n;
	while(n--){
	cin>>sf>>ss;
	len = max(strlen(sf),strlen(ss));
	for(int i = 0;i<len;++i)
		for(int j = 0;j<len;++j)
			mark[i][j]=0;
	cout<<LcsLen(strlen(sf)-1,strlen(ss)-1)<<endl;
	} 
	return 0;
}

C++: #include <iostream>

using namespace std;

int ans(string a, string b) {
  int n = a.size();
  int m = b.size();
  int memo[n+1][m+1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      memo[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int res = max(memo[i-1][j], memo[i][j-1]);
      if (a[i] == b[j]) {
        res = max(res, memo[i-1][j-1] + 1);
      } else {
        res = max(res, memo[i-1][j-1]);
      }
      memo[i][j] = res;
    }
  }
  
  return memo[n][m];
}

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

  for (int i = 0; i < q; i++) {
    string a, b;
    cin >> a >> b;
    
    cout << ans(a, b) << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main()
{
	int kurikaeshi;
	vector<int> answer;
	cin >> kurikaeshi;
	for (int variable = 0;variable < kurikaeshi;++variable) {
		char X[1024], Y[1024];
		int LX = 0, LY = 0;
		vector<vector<int>> L;
		cin >> X >> Y;
		for (int i = 0;X[i] != 0;++i) LX += 1;
		for (int j = 0;Y[j] != 0;++j) LY += 1;
		L.resize(LX+1);
		for (int k = 0;k < LX + 1;++k) L[k].resize(LY+1);
		for (int i = 0;i<LX+1;++i) {
			for (int j = 0;j<LY+1;++j) {
				if (i == 0 || j == 0) {
					L[i][j] = 0;
				}
				else if (X[i-1] == Y[j-1]) {
					L[i][j] = L[i][j-1] + 1;
				}
				else {
					L[i][j] = L[i-1][j] > L[i][j-1] ? L[i-1][j] : L[i][j-1];
				}
			}
		}
		answer.push_back(L[LX][LY]);
	}
	for (int variable = 0;variable < kurikaeshi;++variable) {
		cout << answer[variable] << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<bitset>
#include<iomanip>
#include<cctype>
using namespace std;
const int inf = 1001001000;

void print(vector<vector<int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

int main(){
    int q; cin >> q;
    for (int i = 0; i < q; i++){
        string a, b; cin >> a >> b;
        vector<vector<int>> dp(a.size()+1,vector<int>(b.size()+1,0));
        for (int k = 1; k < a.size()+1; k++){
            for (int l = 1; l < b.size()+1; l++){
                if (a[k-1] == b[l-1]){
                    dp[k][l] = max({dp[k-1][l-1]+1,dp[k-1][l],dp[k][l-1]});
                }
                else dp[k][l] = max(dp[k-1][l],dp[k][l-1]);
            }
        }
        cout << dp[a.size()][b.size()] << endl;
    }

    return 0;
}