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;
#define MAX 1001
int dp[MAX][MAX];
int lcs(string& s,string& t,int si,int ti){
	int ret = 0;
	if(si < 0 || ti < 0)return 0;
	if(dp[si][ti] != -1)return dp[si][ti];
	if( s[si] == t[ti] ){
		ret = max(ret, lcs(s,t,si-1,ti-1) + 1);
	}else{
		if(si > 0)ret = max(ret, lcs(s,t,si-1,ti));
		if(ti > 0)ret = max(ret, lcs(s,t,si,ti-1));
	}
	return dp[si][ti] = ret;
}

int main(void){
	int T;
	cin >> T;
	for(int k=0;k<T;k++){
		string s,t;
		cin >> s >> t;
		// memset(dp,0,sizeof(dp));
		for(int i=0;i<MAX;i++)for(int j=0;j<MAX;j++)
			dp[i][j] = -1;
		cout << lcs(s,t,s.size()-1,t.size()-1) << endl;;
	}
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long

const long long MOD = 1000000007;

int main(){
  int q;
  cin >> q;
  rep(x,q){
      string s,t;
      cin >> s >> t;

     int a=s.size(),b=t.size();

     vector<vector<int>> dp(a+1,vector<int>(b+1,0));
     
     rep(i,a){
         rep(j,b){
             if(s.at(i)==t.at(j)) dp.at(i+1).at(j+1)=dp.at(i).at(j)+1;
             else dp.at(i+1).at(j+1) = max(dp.at(i+1).at(j),dp.at(i).at(j+1)); 
         }
     }
     cout << dp[a][b] << endl;
  }
}

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
 int point;

  for(int i=0;i<n;i++){
    ans[i]=0;
  }

  for(int m=0;m<n;m++){
  cin>>X>>Y;
 int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=0;
}
  }

 
 

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  // ans[l]=N[X.size()][Y.size()];

  cout<<N[X.size()][Y.size()]<<endl;
  // l++;

  }
  //  cout<<ans[0]<<endl;
  // cout<<ans[1]<<endl;
  //cout<<ans[2]<<endl;

 return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

class TLcsSolver
{
    int FTable[1000][1000];
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::scanf("%d", &n);
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::fgets(a, 1000, stdin);
        std::fgets(b, 1000, stdin);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)


int main(){
    int q;
    cin >> q;
    rep(i, q){
        string A, B;
        cin >> A >> B;
        int la = A.length(), lb = B.length();
        vector<vector<int> > dp(la+1, vector<int>(lb+1, 0));
        rep(i, la){
            rep(j , lb){
                if(A.at(i) == B.at(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[la][lb] << endl;
    }
    
}

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

using namespace std;
typedef long long lint;

int dp[1111][1111];

void chmax(int& x, const int y) {
    x = max(x, y);
}

int main() {
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        string X, Y;
        cin >> X >> Y;
        const int N = X.size(), M = Y.size();
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= M; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                // X を進める
                chmax(dp[i+1][j], dp[i][j]);
                // Y を進める
                chmax(dp[i][j+1], dp[i][j]);
                // 同じ文字の場合はどちらも進めて +1
                if (X[i] == Y[j]) {
                    chmax(dp[i+1][j+1], dp[i][j]+1);
                }
            }
        }
        cout << max({dp[N][M], dp[N-1][M], dp[N][M-1]}) << endl;
    }
    return 0;
}