Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

C++: #include<stdio.h>

int hantei(char x[], char y[]) {
  int xn = 0, yn = 0, flag = 0, i, j, max = 0;
  int c[1001][1001];
  
  while (flag == 0)
  {
    if (x[xn] >= 97 && x[xn] <= 122)
    {
      xn++;
    }
    else
    {
      flag = 1;
    }
  }
  
  flag = 0;
  
  while (flag == 0)
  {
    if (y[yn] >= 97 && y[yn] <= 122)
    {
      yn++;
    }
    else
    {
      flag = 1;
    }
  }
  for (i = 1; i < xn + 1; i++) c[i][0] = 0;
  for (j = 1; j < yn + 1; j++) c[0][j] = 0;
  
  for (i = 1; i < xn + 1; i++)
  {
    for (j = 1; j < yn + 1; j++)
    {
      if (x[i - 1] == y[j - 1])
      {
        c[i][j] = c[i - 1][j - 1] + 1;
      }
      else if (c[i - 1][j] >= c[i][j - 1])
      {
        c[i][j] = c[i - 1][j];
      }
      else
      {
        c[i][j] = c[i][j - 1];
      }
      if (max < c[i][j]) max = c[i][j];
    }
  }
  
  return max;
}

int main(void) {
  char x[1000], y[1000];
  int q, i;
  
  scanf("%d", &q);
  
  for (i = 0; i < q; i++)
  {
    scanf("%s", x);
    scanf("%s", y);
    printf("%d\n", hantei(x, y));
  }
  
  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();
    x = ' ' + x;
    y = ' ' + y;
    for (int i = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 0; 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]);
            }
        }
    }

    return c[m][n];
}

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

C++: #include <iostream>
#include<string>
using namespace std;
int main(void){
    // Your code here!
    // Your code here!
    int q;
    cin>>q;
    string x[q],y[q];
   
    //string s[q*2];
    string tmp;
    for(int i=0;i<q;i++){
        cin>>tmp;
        x[i]=tmp;
        cin>>tmp;
        y[i]=tmp;
    }
    for(int i=0;i<q;i++){
        //cout<<sizeof(x)/sizeof(x[0])<<" "<<sizeof(y[i])<<"\n";
        int dp[x[i].length()+1][y[i].length()+1];
        for(int j=0;j<x[i].length()+1;j++){ 
            for(int k=0;k<y[i].length()+1;k++){ 
                if(j==0 || k==0){
                    dp[j][k]=0;
                }
                else if(x[i][j-1]==y[i][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[x[i].length()][y[i].length()]<<"\n";
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int> ans(n);
	for (int cou = 0;cou < n;cou++) {
		string s, t;
		cin >> s >> t;
		int S = s.size(), T = t.size();
		vector<vector<int> > dp(S + 1, vector<int>(T + 1, 0));
		for (int i = 0;i < S;i++) {
			for (int j = 0;j < T;j++) {
				if (s[i] == t[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]);
				}
			}
		}
		ans[cou] = dp[S][T];
	}
	for (int i = 0;i < n;i++) {
		cout << ans[i] << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T>
bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

vvi dp;
string gx, gy;
int dfs(int ix, int iy) {
    if (ix == gx.length() or iy == gy.length()) {
        return 0;
    }
    if (dp[ix][iy] != -1) return dp[ix][iy];

    if (gx[ix] != gy[iy]) {
        int c1 = dfs(ix+1, iy);
        int c2 = dfs(ix, iy+1);
        dp[ix][iy] = max(c1, c2);
    } else {
        int c1 = dfs(ix+1, iy+1)+1;
        int c2 = dfs(ix, iy+1);
        dp[ix][iy] = max(c1, c2);
    }

    return dp[ix][iy];
}

void solve(string x, string y) {
    gx = x, gy = y;
    dp = vvi(1005, vi(1000, -1));
    cout << dfs(0, 0) << endl;
}

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

    int n;
    cin >> n;
    REP(i, n) {
        string x, y;
        cin >> x >> y;
        solve(x, y);
    }

    return 0;
}