Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = " " + s1 , " " + s2
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)
#define all(v) (v).begin(),(v).end()

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

struct edge{
    int from;
    int to;
    int weight;
    edge(int f,int t,int w) : from(f), to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

void solve(){
    string s,t;
    cin >> s >> t;
    int n = s.size();
    int m = t.size();
    vector<vector<int>> dp(n+1,vector<int>(m+1,0));
    rep(i,n) rep(j,m){
        if(s[i]==t[j]){
            dp[i+1][j+1] = max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
        }
        else{
            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
        }
    }
    cout << dp[n][m] << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    rep(_,t) solve();
}

C++: #include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstring>
using namespace std;

string lcs_f;
string lcs_s;
int maxnum = 0;

void lcs() {
    maxnum = 0;
	int flen = lcs_f.size() + 1;
	int slen = lcs_s.size() + 1;
	int a[1001][1001] ={};
	for (int i = 0; i < flen; i++) {
		for (int j = 0; j < slen; j++) {
			if (i == 0 || j == 0) {
				a[i][j] = 0;
			}
			else if (lcs_f[i-1] == lcs_s[j-1]) {
				a[i][j] = a[i - 1][j - 1] + 1;
				if (a[i][j] > maxnum) {
				    maxnum = a[i][j];
				}
			}
			else {
				if (a[i][j - 1] < a[i - 1][j]) {
					a[i][j] = a[i - 1][j];
				}
				else {
					a[i][j] = a[i][j-1];
				}
				 
			}
		}
	}
	
	
}

int main() {
	int n;
	
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> lcs_f >> lcs_s;
		lcs();
		cout << maxnum << endl;
	}
}

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

using namespace std;

int main() {
		
	long n;
	cin >> n ;

	for (long i = 0; i < n; i++) {
		string x, y;
		cin >> x >> y;
		long xl, yl;
		xl = x.size();
		yl = y.size();
		vector<vector<long>> dp(xl+1, vector<long> (yl+1,0));
		for (long j = 0; j < xl; j++) {
			for (long k = 0; k < yl; k++) {

				if (x[j] == y[k]) {
					dp[j+1][k+1] =dp[j][k]+1;
				}
				else{
					dp[j + 1][k + 1] = max(dp[j][k + 1], dp[j + 1][k]);
				}
			}
		}
		cout << dp[xl][yl] << endl;
	}

	return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;

#define STR_MAX 1000

int mat[STR_MAX+1][STR_MAX+1];

int common_subsequence(int len1, int len2, string str1, string str2){
  int i, j;

  for(i=1; i<=len1; i++){
    for(j=1; j<=len2; j++){
      if(str1[i-1] == str2[j-1]) mat[i][j]=mat[i-1][j-1]+1;
      else mat[i][j]=max(mat[i-1][j], mat[i][j-1]);
    }
  }

  return mat[len1][len2];
}

int main(void){
  int i, n;
  string str1, str2;

  scanf("%d", &n);
  for(i=0; i<n; i++){
    cin >> str1 >> str2;

    int len1=str1.size(), len2=str2.size();
    printf("%d\n", common_subsequence(len1, len2, str1, str2));
  }
  
  return 0;
}

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

#define MAX 1000

int lcs(string X, string Y){
    int c[MAX + 1][MAX + 1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++) c[i][0] = 0;
    for(int i = 1; i <= n; i++) c[0][i] = 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 n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << 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++){
    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 n; cin > n;
  for ( int i = 0; i < n; i++ ){
    cin >> s1 >>s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}

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

int lcs(string X, string Y) {
  int max = 0;
  int n = X.size();
  int m = Y.size();
  int dp[1000+1][1000+1];

  X = ' '+X;
  Y = ' '+Y;

  for(int i = 1;i <= n; i++)
    dp[i][0] = 0;

  for(int i = 1; i <= m; i++)
    dp[0][i] = 0;

  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(X[i] == Y[j])
	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];

      if(max < dp[i][j])
	max = dp[i][j];
    }
  }

  return max;
}

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

  string X, Y;
  for (int i = 0; i < N; i++) {
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }
  return 0;
}