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] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// 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<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;
  Y = ' ' + Y;
  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] = mx(c[i - 1][j], c[i][j = 1]);
      }
      maxl = max(mxl, 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 << lcs8s1, s2) << endl;
  }
  return 0;
}

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

int main() {
  
 int q;
 cin >> q;

 for(int i = 0;i < q;i++) {
   string X,Y;
   cin >> X >> Y;
   int x = X.size(),y = Y.size();

   int dp[x+1][y+1];
   for(int i = 0;i <= x;i++) {
     dp[i][0] = 0;
   }
   for(int i = 0;i <= y;i++) {
     dp[0][i] = 0;
   }

   for(int i = 1;i<=x;i++) {
     for(int j = 1;j <= y;j++) {
       if(X.at(i-1) == Y.at(j-1)) dp[i][j] = dp[i-1][j-1] + 1;
       else dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
     }
   }

   cout << dp[x][y] << endl;

 }

  
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 1005;
char inputa[MAXN], inputb[MAXN];
int f[MAXN][MAXN];
int main(void)
{
    int t, lengtha, lengthb;
    scanf("%d", &t);

    while(t--)
    {
        scanf("%s%s", inputa, inputb);
        memset(f, 0, sizeof(f));

        lengtha = strlen(inputa);
        lengthb = strlen(inputb);

        for(int i = 0; i <= lengtha; i++)
        {
            for(int j = 0; j <= lengthb; j++)
            {
                if(i == 0 || j == 0)
                    f[i][j] = 0;
                else if(inputa[i - 1] == inputb[j - 1])
                    f[i][j] = f[i - 1][j - 1] + 1;
                else
                    f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            }
        }

        printf("%d\n", f[lengtha][lengthb]);
    }

    return 0;
}

C++: // C++ 14
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>

#define ll long long
#define loop(x, start, end) for(int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)

#define MAX 1000

using namespace std;



int lcs(string &s1, string &s2) {
    int dp[MAX+1][MAX+1];
    int l1 = s1.length();
    int l2 = s2.length();
    int maxl = 0;
    
    s1 = ' ' + s1;
    s2 = ' ' + s2;
    
    loop(i, 0, l1+1) {
        dp[i][0] = 0;
    }
    loop(j, 0, l2+1) {
        dp[0][j] = 0;
    }
    
    loop (i, 1, l1+1) {
        loop (j, 1, l2+1) {
            if (s1[i] == s2[j]) dp[i][j] = 1 + dp[i-1][j-1];
            else dp[i][j] = max(
                dp[i-1][j],
                dp[i][j-1]
            );
            
            maxl = max(maxl, dp[i][j]);
        }
        
    }
    
    return maxl;
}

int main(void) {
    int n;
    string s1, s2;
    
    cin >> n;
    loop(i, 0, n) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,j) for (int (i)=0;(i)<(int)(j);++(i))
#define put(i) cout << i << endl
using namespace std;
const int MAX = 1000;

int n;
string s1, s2;
int dp[MAX+1][MAX+1];

int rec(int x, int y) {
    if (dp[y][x] >= 0) {
        return dp[y][x];
    }
    int sz1 = s1.size(), sz2 = s2.size();
    int ret;
    if (x == sz1 || y == sz2) {
        ret = 0;
    }
    else if (s1[x] == s2[y]) {
        ret = rec(x+1, y+1)+1;
    }
    else {
        ret = max(rec(x+1, y), rec(x, y+1));
    }
    return dp[y][x] = ret;
}

void solve() {
    while (n--) {
        memset(dp, -1, sizeof(dp));
        cin >> s1 >> s2;
        cout << rec(0, 0) << endl;
    }
}

int main() {
    cin >> n;
    solve();
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <climits>
 
using namespace std;
 
int f(map<string,int>& dmap,const string& dstr1,string dstr2);
 
int main()
{
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        string data[2];
        map<string,int> dmap;
        cin>>data[0]>>data[1];
        if(data[0].length()<data[1].length()){
            cout<<f(dmap,data[1],data[0])<<endl;
        }else{
            cout<<f(dmap,data[0],data[1])<<endl;
        }
    }
}
int f(map<string,int>& dmap,const string& dstr1,string dstr2)
{
    auto dit=dmap.find(dstr2);
    if(dit!=dmap.end()){
        return (*dit).second;
    }
    int j=0;
    for(int i=0;i<dstr1.length()&&j<dstr2.length();i++,j++){
        while(i<dstr1.length()&&dstr1[i]!=dstr2[j]){
            i++;
        }
    }
    if(j==dstr2.length()){
        dmap.insert(pair<string,int>(dstr2,(int)dstr2.length()));
        return dstr2.length();
    }
    if(dstr2.length()==1){
        dmap.insert(pair<string,int>(dstr2,0));
        return 0;
    }
    f(dmap,dstr1,dstr2.substr(1,dstr2.length()-1));
    f(dmap,dstr1,dstr2.substr(0,dstr2.length()-1));
    j=0;
    for(int i=2;i<dstr2.length();i++){
        int c=f(dmap,dstr1,dstr2.substr(0,i-1)+dstr2.substr(i,dstr2.length()-i));
        if(j<c){
            j=c;
        }
    }
    dmap.insert(pair<string,int>(dstr2,j));
    return j;
}