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 <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>

using namespace std;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = n;i >= m;i--)
#define REPO(i, n) for(int i = 1;i <= n;i++)
#define ll long long
#define INF 999999999
#define MINF -999999999
#define ALL(n) n.begin(),n.end()
#define MP make_pair
#define F first
#define S second
int dp[1100][1100]; 

int main() {
	int n;
	string a, b;
	cin >> n;
	REP(z, n) {
		cin >> a >> b;
		REP(i, 1100) REP(j, 1100) dp[i][j] = 0;
		REP(i, a.length()) {
			REP(j, b.length()) {
				if (a[i] == b[j]) dp[i + 1][j + 1] = max(dp[i][j] + 1, max(dp[i][j + 1], dp[i + 1][j]));
				else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
			}
		}
		cout << dp[a.length()][b.length()] << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algoritm>
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>
#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;//??¨X[0]????????\?????????
	Y=' '+Y;//??¨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]);
		}
	}
	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;
	}
}

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

using namespace std;

class Dataset{
private:
 string X;
 string Y;
public:
 Dataset(){
  string tmp = "";
  cin >> tmp; X = tmp; X = " " + X;
  cin >> tmp; Y = tmp; Y = " " + Y;
 };

 unsigned long getLongestCommonLength(){
  unsigned long Xsize = X.size();
  unsigned long Ysize = Y.size();
 
  vector< vector<unsigned long> > LC(Xsize, vector<unsigned long>(Ysize, 0));

  for(unsigned long X_i = 1; X_i < Xsize; ++X_i){
   for(unsigned long Y_i = 1; Y_i < Ysize; ++Y_i){
    unsigned long tmp = LC[X_i][Y_i-1];
    if(X[X_i] == Y[Y_i]) ++tmp;
    if(tmp < LC[X_i-1][Y_i]) tmp = LC[X_i-1][Y_i];
    LC[X_i][Y_i] = tmp;
   }
  }

  return LC[Xsize-1][Ysize-1];
 };


protected:
};

int main(){
 long datasetNum = 0; cin >> datasetNum;
 for(unsigned long dataset_i = 0; dataset_i < datasetNum; ++dataset_i){
  Dataset data;
  cout << data.getLongestCommonLength() << endl;
 }

 return 0;
};

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

int main(){
    int q;
    cin >> q;
    rep(ss,q){
        string x,y;
        cin >> x >> y;
        int xlen = x.size();
        int ylen = y.size();
        int dp[1001][1001];//dp[i][j]xi,yjまででのlcs
        memset(dp,0,sizeof(dp));
        rep(i,xlen)rep(j,ylen){
            if(x[i]==y[j]){
                dp[i+1][j+1] = max(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[xlen][ylen] << endl;
    }
}

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

int lcs(string &x,string &y);

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

int lcs(string &x,string &y)
{
	const int maxlen=1000+24;
	vector<vector<int>> dp(maxlen,vector<int>(maxlen));

	int lenx=x.size();
	int leny=y.size();
	for (int i=0;i<=lenx;++i)
	{
		dp[i][0]=0;
	}
	for (int j=0;j<=leny;++j)
	{
		dp[0][j]=0;
	}

	for (int i=1;i<=lenx;++i)
	{
		for (int j=1;j<=leny;++j)
		{
			if (x[i-1]==y[j-1])
			{
				dp[i][j]=dp[i-1][j-1]+1;
			}
			else
			{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}

	int maxdp=0;
	for (int j=1;j<=leny;++j)
	{
		maxdp=max(maxdp,dp[lenx][j]);
	}
	return maxdp;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s,ss;
    cin>>s>>ss;
    int a[ss.size()+2][s.size()+2];
    r(i,0,ss.size()+2)r(j,0,s.size()+2)a[i][j]=0;
    ss=' '+ss;
    s=' '+s;
    r(i,1,ss.size()+1){
      r(j,1,s.size()+1){
        if(ss[i]==s[j])a[i][j]=a[i-1][j-1]+1;
        else a[i][j]=max(a[i-1][j],a[i][j-1]);
      }
    }
    cout<<a[ss.size()-1][s.size()-1]<<endl;
  }
}