Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int lcs(string a, string b) {
    vector<vector<int>> dp(a.size(), vector<int>(b.size(), 0));
    //init
    dp[0][0] = (a[0] == b[0]);
    for (auto i = 1; i < (int)a.size(); i++) dp[i][0] = dp[i - 1][0] or (a[i] == b[0]);
    for (auto i = 1; i < (int)b.size(); i++) dp[0][i] = dp[0][i - 1] or (a[0] == b[i]);
    // dp
    for (auto i = 1; i < (int)a.size(); i++) {
        for (auto j = 1; j < (int)b.size(); j++) {
            if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }

//    for (auto i = 0; i < (int)b.size(); i++) {
//        for (auto j = 0; j < (int)a.size(); j++) {
//            cout << dp[j][i] << " ";
//        }
//        cout << endl;
//    }
    return dp[a.size() - 1][b.size() - 1];
}

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

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

    return 0;
}

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;
}