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(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	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 <bits/stdc++.h>
using namespace std;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

vector<vector<int>> dp(2000, vector<int>(2000));

void solve(string s, string t){
  int n = int(s.size());
  int m = int(t.size());

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; 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]);
      }
    }
  }

  cout << dp[n][m] << endl;
}

int main(){
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string s, t;
    cin >> s >> t;
    solve(s, t);
  }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int N = 1000;
void lcs(string x, string y)
{
    int m = x.length(), n = y.length();
    int c[1001][1001]; //c[0][0] = 0;
    int maxv = 0;
    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]);
            maxv = max(maxv, c[i][j]);
        }
    }
    printf("%d\n", maxv);
}
int main()
{
    int q; string x, y;
    cin >> q;
    for(int i=0; i<q; i++)
    {
        cin >> x >> y;
        lcs(x, y);
    }
    //system("pause");
    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 = 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]);
            }
            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<stdio.h>
#include<string.h>
#define max(a,b) ((a)>(b) ? (a):(b))
int dp[1001][1001];
int lcs(char *s,char *t){
    int i,j,ls,lt;

    ls=strlen(s);
    lt=strlen(t);
    for(i=0;i<ls;i++){
	for(j=0;j<lt;j++){
	    if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1;
	    else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
	}
    }
    return dp[ls][lt];
}
int main(){
    char s[1001],t[1001];
    int k;

    scanf("%d", &k);
    while(k--){
	scanf("%s%s", s, t);
	printf("%d\n",lcs(s,t));
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
 
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A); 
strB = strlen(B);

    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
 
  return 0;
}

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


int S[1001][1001];

void prep(string str1, string str2, int len1, int len2)
{
	int a, b, i;
	S[0][0] = 0;
	for(i = 1; i <= len1; i++){ S[i][0] = 0; }
	for(i = 1; i <= len2; i++){ S[0][i] = 0; }
	for(a = 1; a <= len1; a++){
		for(b = 1; b <= len2; b++){
			if(str1[a - 1] == str2[b - 1]){ S[a][b] = 1; }else{ S[a][b] = 0; }
		}
	}
	return;
}

int main()
{
	int i, a, b, q, len1, len2;
	cin >> q;
	string str1, str2;

	for(i = 0; i < q; i++){
		cin >> str1 >> str2;
		len1 = str1.size();
		len2 = str2.size();
		prep(str1, str2, len1, len2);
		for(a = 1; a <= len1; a++){
			for(b = 1; b <= len2; b++){
				if(S[a][b]){ S[a][b] = S[a - 1][b - 1] + 1; }
				else{ S[a][b] = max(S[a - 1][b], S[a][b - 1]); }
			}
		}
		cout << S[len1][len2] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i, p, n) for(int i = p; i < n; i++) 

int main() {
    int n;
    cin >> n;
    rep(i, 0, n) {
        string s, t;
        cin >> s >> t;
        int lcs[s.size()+1][t.size()+1];
        rep(j, 0, s.size()+1) {
            rep(k, 0, t.size()+1) {
                lcs[j][k] = 0;
            }
        }
        rep(j, 1, s.size()+1) {
            rep(k, 1, t.size()+1) {
                if(s[j-1] == t[k-1]) {
                    lcs[j][k] = max(lcs[j-1][k], max(lcs[j][k-1], lcs[j-1][k-1]+1));
                }
                else {
                    lcs[j][k] = max(lcs[j-1][k], lcs[j][k-1]);
                }
            }
        }

        cout << lcs[s.size()][t.size()] << endl;
        
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

string s1,s2;

int L(int i,int j){
	if(i<0||j<0)return 0;
	else if(s1.at(i)==s2.at(j))return 1+L(i-1,j-1);
	else{
		int a=L(i,j-1);
		int b=L(i-1,j);
		return a>b?a:b;
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		cout<<L(s1.size()-1,s2.size()-1)<<endl;
	}
}