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 <cmath>
#include <vector>

constexpr int MAX = 1000;
char ls[MAX];
char rs[MAX];
int table[MAX + 1][MAX + 1];


void calc_lcs() {
    scanf("%s", ls);
    scanf("%s", rs);
    int l_len = int(std::strlen(ls));
    int r_len = int(std::strlen(rs));
    for (int i = 0; i < l_len; i++) {
        table[i][0] = 0;
    }
    for (int i = 0; i < r_len; i++) {
        table[0][i] = 0;
    }

    for (int l_i = 1; l_i < l_len + 1; l_i++) {
        for (int r_i = 1; r_i < r_len + 1; r_i++) {
            if (ls[l_i - 1] == rs[r_i - 1]) {
                table[l_i][r_i] = table[l_i - 1][r_i - 1] + 1;
            } else {
                table[l_i][r_i] = std::max(table[l_i][r_i - 1], table[l_i - 1][r_i]);
            }
        }
    }
    std::cout << table[l_len][r_len] << std::endl;
}

int main() {
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        calc_lcs();
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(n,a) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265


void f(){
  string x,y;
  cin>>x>>y;
  int dp[1100][1100]={};
  
  rep(i,x.length()+1){
    bool flag=false;
    rep(j,y.length()+1){
      if(i==0||j==0) continue;
      int z= max(dp[i-1][j],dp[i][j-1]);
      if(!flag && x[i-1]==y[j-1]){
        z++;
        flag = true;
      }
      dp[i][j]=max(dp[i][j],z);
    }
  }

  cout<<dp[(int)x.length()][(int)y.length()]<<endl;
}


int main(){
  int q;
  cin>>q;
  while(q--){
    f();
  }
}

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

using namespace std;

int calc_longest_subseq(string, string);

int main() {
	int q;
	cin >> q;
	string msg1, msg2;
	for (int i = 0;i < q;i++) {
		cin >> msg1>>msg2;
		cout << calc_longest_subseq(msg1, msg2) << endl;
	}
}

int calc_longest_subseq(string a, string b) {
	int* last_index = new int[b.length()+1];
	pair<char, int>* sorted_a = new pair<char, int>[a.length()];
	int tmp;
	for (int i = 0;i < a.length();i++) {
		sorted_a[i] = make_pair(a[i], i);
	}
	sort(sorted_a, sorted_a + a.length());
	last_index[0] = -1;
	int count = 0;
	for (int i = 1;i <= b.length();i++) {
		last_index[i] = INT_MAX;
	}
	for (int i = 0;i < b.length();i++) {
		for (int j = count;j >= 0;j--) {
			tmp = upper_bound(sorted_a, sorted_a + a.length(), make_pair(b[i], last_index[j]))-sorted_a;
			if (tmp < a.length()) {
				if (sorted_a[tmp].first == b[i]) {
					last_index[j + 1] = min(last_index[j + 1], sorted_a[tmp].second);
					count = max(j + 1, count);
				}
			}
		}
	}
	return count;

}

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


int main() {
	int n, m, q;
	string a, b;//two sequences
	int M[1001][1001] = { { 0 } };//Memory matrix

	cin >> q;
	for (int l = 0; l < q; l++)
	{
		cin >> a >> b;
		n = a.length();
		m = b.length();
		for (int i = 0; i <= n; i++)
		{
			for (int j = 0; j <= m; j++)
			{
				if (a[i] == b[j])
				{
					M[i + 1][j + 1] = M[i][j] + 1;
				}
				else
				{
					M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
				}
			}
		}

		cout << M[n][m] << endl;//The time complexity equals to O (n * m)
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
#include <cctype>

using namespace std;

#define REP(i,n) for(int i = 0; i < (int)n; ++i)

string X, Y;

void solve(int x_size, int y_size)
{
	vector< vector<int> > dp(x_size + 1, vector<int>(y_size + 1));

	for(int i = 0; i < x_size; ++i) {
		for(int j = 0; j < y_size; ++j) {
			if(X[i] == Y[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[X.size()][Y.size()] << endl;
}

int main()
{
	int n;
	cin >> n;
	REP(i, n) {
		cin >> X >> Y;
		solve(X.size(), Y.size());
	}
	return 0;
}

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

int max(int a, int b, int c);

int main(){
    int n;
    std::cin >> n;
    std::string str1;
    std::string str2;
    for(int i=0;i<n;i++){
        std::cin >> str1;
        std::cin >> str2;
        int size1 = str1.size();
        int size2 = str2.size();
        int dp[size1+1][size2+1];
        dp[0][0] = 0;
        for(int j=0;j<=size1;j++){
            dp[j][0] = 0;
        }
        for(int j=0;j<=size2;j++){
            dp[0][j] = 0;
        }

        for(int j=0;j<size1;j++){
            for(int k=0;k<size2;k++){
                if(str1[j]==str2[k]){
                    dp[j+1][k+1] = max(dp[j][k+1],dp[j][k] + 1,dp[j+1][k]);
                }else{
                    dp[j+1][k+1] = max(dp[j][k+1],dp[j][k],dp[j+1][k]);
                }
            }
        }

        std::cout << dp[size1][size2] << std::endl;
    }
    return 0;
}

int max(int a, int b, int c){
    if(a>b){
        if(a>c){
            return a;
        }else{
            return c;
        }
    }else{
        if(b>c){
            return b;
        }else{
            return c;
        }
    }
}