Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main(){
  int n,dp[1010][1010],i,j,k,a,b;
  scanf("%d\n",&n);
  for(k=0;k<n;k++){
    string x,y;
    cin >> x >> y;
    a=x.size(),b=y.size();
    for(i=0;i<=a;i++){
      for(j=0;j<=b;j++){
        dp[i][j]=0;
      }
    }
    for(i=1;i<=a;i++){
      for(j=1;j<=b;j++){
        if(x[i-1]==y[j-1]){
          dp[i][j]=dp[i-1][j-1]+1;
        }
        else{
          if(dp[i-1][j]>dp[i][j-1]){
            dp[i][j]=dp[i-1][j];
          }
          else{
            dp[i][j]=dp[i][j-1];
          }
        }
      }
    }
      printf("%d\n",dp[a][b]);
    }
  return 0;
}

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

int dp[1005][1005];
int main(){
	int q;
	cin >> q;
	rep(i,q) {
		string s, t;
		cin >> s >> t;
		int N = s.size();
		int M = t.size();
		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]);
				}
			}
		}
		cout << dp[N][M] << endl;
	}
	
    return 0;
}

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

using namespace std;

int solve(string s1, string s2){
    int L[s1.size()+1][s2.size()+1];
    for(int i=0;i<=s1.size();i++){
        for(int j=0;j<=s2.size();j++){
            if(i==0 || j==0){
                L[i][j]=0;
            }
            else if(s1[i] == s2[j]){
                L[i][j] = 1 + L[i-1][j-1];
            }
            else{
                L[i][j] = max(L[i][j-1],L[i-1][j]);
            }
        }
    }
    return L[s1.size()][s2.size()];
}

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

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


long long calc(int i, int j, vector<vector<long long> > &l, const string& x, const string& y){
  if(i == 0 || j == 0){
    l[i][j] = 0;
    return l[i][j];
  }
  if(l[i][j] >= 0){
    return l[i][j];
  }else{
    if(x[i-1] == y[j-1]){
      l[i][j] = 1 + calc(i-1, j-1, l, x, y);
    }else{
      l[i][j] = max(calc(i-1, j, l, x, y), calc(i, j-1, l, x, y));
    }
    return l[i][j];
  }
}
int main(){
  int n;
  cin >> n;
  for(int i = 0; i < n; i++){
    string x,y;
    cin >> x >> y;
    vector<vector<long long> > l(x.length()+1, vector<long long>(y.length()+1, -1));
    cout << calc(x.length(), y.length(), l, x, y) << endl;
  }
}

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

int lcs(string s, string t){
        int a[s.size()+1][t.size()+1];
        for(int i=0;i<s.size()+1;i++) a[i][0]=0;
        for(int i=0;i<t.size()+1;i++) a[0][i]=0;
        for(int i=1;i<s.size()+1;i++){
                for(int j=1;j<t.size()+1;j++){
                        if(s[i-1]==t[j-1]) a[i][j] = a[i-1][j-1]+1;
                        else a[i][j] = max(a[i-1][j], a[i][j-1]);
                }
        }
        return a[s.size()][t.size()];
}

int main(){
        string s,t;
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
                cin>>s>>t;
                cout<<lcs(s,t)<<endl;
        }
        return 0;
}

C++: // 5/5
// 2-3-2. 最長共通部分裂問題の類題

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

int dp[1010][1010];

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

    int n, m;
    // cin >> n >> m;

    vector<int> ans;

    rep(p, q) {
        string s, t;
        cin >> s >> t;

        n = s.size();
        m = t.size();
        memset(dp, 0, sizeof(dp));

        dp[0][0] = 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][j + 1], dp[i + 1][j]);
            }
        }

        // cout << dp[n][m] << endl;
        ans.push_back(dp[n][m]);
    }

    for (auto v : ans) cout << v << endl;
}

C++: #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

int dp[1'001][1'001];

void solve(const string& x, const string& y)
{
    irep(i, x.size()) irep(j, y.size())
    {
        if (i == 0 || j == 0) {
            dp[i][j] = 0;
        } else {
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            if (x[i - 1] == y[j - 1]) {
                dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
            }
        }
    }
    cout << dp[x.size()][y.size()] << endl;
}

int main()
{
    int n;
    cin >> n;
    rep(i, n)
    {
        string x, y;
        cin >> x >> y;
        solve(x, y);
    }
    return 0;
}