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;
static const int N=1001;
int lcs(string& X,string& Y){
  int c[N][N] = {};
  int m=X.size();
  int n=Y.size();

  int max1=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]);
      }
      max1=max(max1,c[i][j]);
    }
  }
  return max1;
}

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<vector>
#include<string>
#include<algorithm>

using namespace std;

string s, t;

int main() {
	int n;
	cin >> n;
	while (n--) {
		//dp[i][j] := s???i??????????????§???t???j??????????????§?????£?????¨??????LCS
		vector<vector<int>> dp(2000, vector<int>(2000));
		cin >> s >> t;
		dp[0][0] = (s[0] == t[0]);
		for (int i = 1; i < s.size(); i++)dp[i][0] = (bool)(dp[i-1][0] + (s[i] == t[0]));
		for (int j = 1; j < t.size(); j++)dp[0][j] = (bool)(dp[0][j-1] + (s[0] == t[j]));
		
		for (int i = 1; i < s.size(); i++) {
			for (int j = 1; j < t.size(); j++) {
				if (s[i] == t[j])dp[i][j] = dp[i-1][j-1]+1;
				else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		cout << dp[s.size() - 1][t.size() - 1] << endl;
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int num = 1001;
int lg[num + 1][num + 1];
int main() {
	int w;
	string s, t;
	cin >> w;
	for (int i = 0; i < w; i++) {
		cin >> s;
		cin >> t;
		int n = s.size();
		int m = t.size();
		for (int i = 0; i < n; i++) lg[i][0] = 0;
		for (int i = 0; i < m; i++) lg[0][i] = 0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				if (s[i] == t[j]) lg[i + 1][j + 1] = lg[i][j] + 1;
				else lg[i + 1][j + 1] = max(lg[i][j + 1], lg[i + 1][j]);
			}
		cout << lg[n][m] << endl;
	}
	return 0;
	/*int n, m;
	char s[num], t[num];
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> s[i];
	for (int i = 0; i < m; i++) cin >> t[i];
	for (int i = 0; i < n; i++) lg[i][0] = 0;
	for (int i = 0; i < m; i++) lg[0][i] = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			if (s[i] == t[j]) lg[i + 1][j + 1] = lg[i][j] + 1;
			else lg[i + 1][j + 1] = max(lg[i][j + 1], lg[i + 1][j]);
		}
	cout << lg[n][m] << endl;
	return 0;*/
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template<class T> using vv=vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;


int main() {
    int q;
    cin >> q;
    rep(i,q) {
        string x,y;
        cin >> x >> y;
        int dp[1050][1050];
        rep(i,(int)x.length()) {
            rep(j,(int)y.length()) {
                if(i == 0 || j == 0) {
                    if(x[i] == y[j]) {
                        dp[i][j] = 1;
                    }
                } else if(x[i] == y[j]) {
                    dp[i][j] = dp[i-1][j-1] +1;
                } else {
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        cout << dp[x.length()-1][y.length()-1] << endl;
    }
	return 0;
}

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

int Longest(string x, string y) {
  int L[1001][1001];
  int maxl = 0;
  int xlen = x.size();
  int ylen = y.size();

  x = ' ' + x;
  y = ' ' + y;

  for (int i = 1; i <= xlen; i++) L[i][0] = 0;
  for (int j = 1; j <= ylen; j++) L[0][j] = 0;

  for (int i = 1; i <= xlen; i++) {
    for (int j = 1; j <= ylen; j++) {
      if (x[i] == y[j])
        L[i][j] = L[i-1][j-1] + 1;
      else
        L[i][j] = max(L[i][j-1], L[i-1][j]);

      maxl = max(maxl, L[i][j]);
    }
  }

  return maxl;
}

int main() {
  string X, Y;
  int n;

  cin >> n;

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

  return 0;
}

C++: #include <bits/stdc++.h>
#define MAX 1000
using namespace std;
int lcs(string,string);

int main(){
  int i,n;
  string st1,st2;

  cin >> n;
  for(i = 0; i < n; i++){
    cin >> st1 >> st2;
    cout << lcs(st1,st2) << endl;
  }
  return 0;
}

int lcs(string st1,string st2){
  int preMax = 0;
  int m,n,i,j;
  int key[MAX+1][MAX+1];

  m = st1.size();
  n = st2.size();
  /**/
  st1 = ' '+st1;
  st2 = ' '+st2;
  for(i = 1; i <= m; i++){
    key[i][0] = 0;
  }
  for(j = 1; j <= n; j++){
    key[0][j] = 0;
  }
  for(i = 1; i <= m; i++){
    for(j = 1; j <= n; j++){
      if(st1[i] == st2[j]){
        key[i][j] = key[i-1][j-1]+1;
      }
      else{
        key[i][j] = max(key[i-1][j],key[i][j-1]);
      }
      preMax = max(preMax,key[i][j]);
    }
  }
  return preMax;
}