Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

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

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

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

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

  return max;
}

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;

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

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

  return 0;
}

C++: #include<bits/stdc++.h>
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.insert(0, " ");  // X[0]に空白を挿入
    Y.insert(0, " ");;  // Y[0]に空白を挿入

    for (int i = 1; i <= m; i++) c[i][0] = 0;
    for (int j = 1; j <= n; j++) c[0][j] = 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]);
            // if (c[i][j] > maxl) {
            //     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], m=X.size(), n=Y.size(), LMAX=0;

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

  for(int i=1; i<=m; i++)
    c[i][0]=0;
  for(int j=1; j<=n; j++)
    c[0][j]=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]);
      }
      LMAX=max(LMAX, c[i][j]);
    }
  }

  return LMAX;
}

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<string>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
	
	vector<int> v;
	int n=0;
	scanf("%d", &n);
	string a;
	string b;
	for (int i = 0; i < n; ++i)
	{
		char a1[1000],b1[1000];
		string a(a1);
		string b(b1);
		vector<vector<int> > dp(int(a.size()), vector<int>(int(b.size())));
		
		if(a[0]==b[0]) dp[0][0]=1;
		for (int i = 0; i < a.size(); ++i)
		{
			if(a[i]==b[0]){
				dp[i][0]=1;
			} else {
				dp[i][0]=dp[i-1][0];
			}
		}
		for (int i = 0; i < b.size(); ++i)
		{
			if(a[0]==b[i]){
				dp[0][i]=1;
			} else {
				dp[0][i]=dp[0][i-1];
			}
		}
		for (int i = 1; i < a.size(); ++i)
		{
			for (int j = 1; j < 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-1],max(dp[i-1][j],dp[i][j-1]));
				}
			}
		}
		int na=int(a.size());
		int nb=int(b.size());
		v.push_back(dp[na][nb]);
	}
	for (int i = 0; i < v.size(); ++i)
	{
		printf("%d\n",v[i]);
	}

	return(0);
}

C++: #include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        string str1,str2;
        cin>>str1>>str2;
        int lenth1,lenth2;
        lenth1=(int)str1.length();
        lenth2=(int)str2.length();
        int dp[lenth1+1][lenth2+1];
        for(int j=0;j<=lenth1;j++)dp[j][0]=0;
        for(int j=0;j<=lenth2;j++)dp[0][j]=0;
        for(int j=1;j<=lenth1;j++)
        {
            for(int k=1;k<=lenth2;k++)
            {
                if(str1[j-1]==str2[k-1])
                {
                    dp[j][k]=dp[j-1][k-1]+1;
                }
                else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
            }
        }
        cout<<dp[lenth1][lenth2]<<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++) c[i][0]=0;
  for(int j=1;j<=n;j++) c[0][j]=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>
using namespace std;

int lcs(string X, string Y) {
  int cs[1000][1000];
  int m = X.size();
  int n = Y.size();
  int answer = 0;
  for (int i = 0; i < m; i++) cs[m][0] = 0;
  for (int i = 0; i < n; i++) cs[0][n] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) {
        cs[i][j] = 1 + cs[i - 1][j - 1];
      } else {
        cs[i][j] = max(cs[i - 1][j], cs[i][j - 1]);
      }
      answer = max(answer, cs[i][j]);
    }
  }

  return answer;
}

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

  int n;
  cin >> n;

  string X, Y;
  for (int i = 0; i < n; i++) {
    cin >> X >> Y;
    cout << lcs(X, Y) << endl;
  }

  return 0;
}

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

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;
using vii=vector<vector<int>>;

const ll LINF=1LL<<60;
const int INF=1<<29;
const int MOD=1e9+7;

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

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

int main(){
    int q; cin >> q;
    while(q--){
        string X, Y;
        cin >> X >> Y;

        vii dp(X.size()+1, vi(Y.size()+1, 0));
        for(int i=0; i<X.size(); i++){
            for(int j=0; j<Y.size(); j++){
                if(X[i] == Y[j]) chmax(dp[i+1][j+1], dp[i][j]+1);
                chmax(dp[i+1][j+1], max(dp[i+1][j], dp[i][j+1]));
            }
        }

        cout << dp[X.size()][Y.size()] << endl;
    }
    return 0;
}