Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

C++: #include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    string x,y;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        int c[x.size()+1][y.size()+1];
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
                if(j==0||k==0)c[j][k]=0;
                else if(j>0&&k>0&&x[j-1]==y[k-1]) c[j][k]=c[j-1][k-1]+1;
                else if(j>0&&k>0&&x[j-1]!=y[k-1])c[j][k]=max(c[j][k-1],c[j-1][k]);
            }
        }
        int maxi=0;
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
               maxi=max(maxi,c[j][k]);
               c[j][k]=0;
            }
        }
        cout<<maxi<<endl;
    }
    
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define endl '\n'
using namespace std;
int dy[4] = { 1, 0, -1, 0 }, dx[4] = { 0, 1, 0, -1 };
typedef pair<ll, ll> P;
const int MAX_N = 1000;
int dp[MAX_N + 1][MAX_N + 1];
string x, y;
int q;

int solve(string x, string y)
{
    int n = x.size(), m = y.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            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()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        cout << solve(x, y) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int Max = 1000;
int getlong(string a, string b){
int n,m,i,j,as=0;
	int cs[Max + 1][Max + 1];
	n=a.size();
	m=b.size();

	a=' '+a;
	b=' '+b;
	cs[0][0]=0;
	for(i=1;i<=n;i++)
	cs[i][0]=0;
	for(i=1;i<=m;i++)
	cs[0][i]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			if(a[i]==b[j])
			cs[i][j]=cs[i-1][j-1]+1;
			else
			cs[i][j]=max(cs[i][j-1],cs[i-1][j]);
		as=max(as,cs[i][j]);
		}
		
	}
	return as;
}
main(){
	int q,i;
	string a,b;
	cin>>q;
	for(i=0;i<q;i++){
		cin>>a>>b;
		printf("%d\n",getlong(a,b));
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, l1, l2;
	string s1, s2;

	cin >> n;
	while (n--) {
		cin >> s1 >> s2;
		l1 = s1.size();
		l2 = s2.size();

		int dp[l1 + 1][l2 + 1];
		for (int i = 0; i <= l1; i++) {
			for (int j = 0; j <= l2; j++) {
				dp[i][j] = 0;
			}
		}
		for (int i = 1; i <= l1; i++) {
			for (int j = 1; j <= l2; j++) {
				if (s1[i - 1] == s2[j - 1]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		cout << dp[l1][l2] << endl;
	}

	return 0;
}

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

using namespace std;
int dp[1002][1002];
int main(){
    int T;
    cin>>T;
    while(T--){
        #ifdef LCS
        string s;
        cin>>s;
        int len=s.size();
        int *dp=new int[len+1];
        dp[0]=1;
        int ans=1;
        for(int i=1;i<len;i++){
            char ch=s[i];
            int cnt=0;
            for(int j=0;j<=i-1;j++){
                if(s[j]<ch) cnt=max(cnt,dp[j]);
            }
            dp[i]=cnt+1;
            ans=max(ans,dp[i]);
        }
        cout<<ans<<endl;
        #endif//LCS
        string s1,s2;
        cin>>s1>>s2;
        int len1=s1.size(),len2=s2.size();
        int len=max(len1,len2);
        for(int i=0;i<=len;i++) {
            dp[0][i]=0;
            dp[i][0]=0;
        }
        int ans=0;
        for(int i=0;i<len1;i++){
            for(int j=0;j<len2;j++){
                if(s1[i]==s2[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[len1][len2]<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;
int dp[1100][1100];

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

    cin >> Q;
    vector<int> ans;

    REP(i, Q) {
        string a, b;
        cin >> a >> b;
        memset(dp, 0, sizeof(dp));

        FOR(i, 1, (int)a.size() + 1) {
            FOR(j, 1, (int)b.size() + 1) {
                if (a[i - 1] == b[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1);
                chmax(dp[i][j], dp[i - 1][j]);
                chmax(dp[i][j], dp[i][j - 1]);
            }
        }
        /*
        REP(i, (int)a.size() + 1) {
            REP(j, (int)b.size() + 1) { cout << dp[i][j] << " "; }
            cout << endl;
        }
        */
        ans.push_back(dp[(int)a.size()][(int)b.size()]);
    }

    for (auto i : ans) {
        cout << i << endl;
    }

    return 0;
}

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

using namespace std;

int lcslen[1001][1001];
int compute_lcslen(const string &x,const string &y);

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

  return 0;
}

int compute_lcslen(const string &x,const string &y){
  for(int i=1;i<=x.length();++i){
    for(int j=1;j<=y.length();++j){
      if(x[i]==y[j]){
        lcslen[i][j] = lcslen[i-1][j-1]+1;
      }else{
        lcslen[i][j] = max(lcslen[i-1][j],lcslen[i][j-1]);
      }
    }
  }
  return lcslen[x.length()][y.length()];
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define MAX 1000

using namespace std;

string s,t;
int dp[MAX + 1][MAX + 1];

int LCS(void){

  for( int i = 0 ; i < s.size() ; i++ ){
    for( int j = 0 ; j < t.size() ; j++ ){
      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]);
      }
    }
  }
  return dp[s.size()][t.size()];
}


int main(void){

  int n;
  cin >> n;
  while(n--){

    memset(dp, 0 , sizeof(dp) );

    cin >> s >> t;
    cout << LCS() << endl;

  }
  return 0;
}