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 <bits/stdc++.h>

using namespace std;
#define MAX 1000

int lcs(string X, string Y) {
  int c[MAX+1][MAX+1];
  int m, n;
  m = X.size();
  n = Y.size();
  int maxlen = 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][j - 1], c[i - 1][j]);
      }
      maxlen = max(maxlen, c[i][j]);
    }
  }
  return maxlen;
}


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

C++: #include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int map[2000][2000];

int main(void) {
    int q;
    scanf("%d",&q);
    for(int i=0; i<q; i++) {
        memset(map, 0, sizeof(map));
        char x[2000], y[2000];
        scanf("%s%s",x,y);
        int xl=strlen(x),yl=strlen(y);

        for(int m=0; m<xl; m++)
            map[m][0] = 0;
        for(int n=0; n<yl; n++)
            map[0][n] = 0;
        for(int m=1; m<=xl; m++)
            for(int n=1; n<=yl; n++) {
                if(x[m-1] == y[n-1])
                    map[m][n] = max(max(map[m-1][n-1]+1, map[m-1][n]), map[m][n-1]);
                else
                    map[m][n] = max(max(map[m-1][n-1]+0, map[m-1][n]), map[m][n-1]);
            }
        printf("%d\n", map[xl][yl]);
    }
    return 0;
}

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

#define REP(i, n) for(int i=0; i<n;i++)
#define REPR(i, n) for(int i=n-1 ; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)

#define INF 2e9
#define ll long long

int main(){
    int n;
    int dp[1001][1001];
    cin>>n;
    int i, j, k;
    int ans = 0;
    for(i=0; i<n; i++){
        string s, v;
        cin>>s>>v;
        int l = s.length();
        int m = v.length();
        for(j=0; j<1001; j++){
            for(k=0; k<1001; k++){
                dp[j][k] = 0;
            }
        }
        for(j=0; j<l; j++){
            for(k=0; k<m; k++){
                if(s[j] == v[k]){
                    dp[j+1][k+1] = dp[j][k]+1;
                }
                else{
                    dp[j+1][k+1] = max(dp[j][k+1], dp[j+1][k]);
                }
            }
        }
        cout<<dp[l][m]<<endl;

    }
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int lcs[1001][1001];
/*int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};*/
int main()
{
    int num;
    cin>>num;
    for(int l=0;l<num;l++)
    {

        for(int i=0;i<1001;i++)
            for(int j=0;j<1001;j++)
                lcs[i][j]=0;
        string a,b;
        cin>>a>>b;
        //cout<<lcs(a,b)<<endl;
        for(int i=0;i<a.length()+1;i++)
        {
            for(int j=0;j<b.length()+1;j++)
            {
                if(i==0||j==0)
                {
                    lcs[i][j]=0;
                }
                else if(a[i-1]==b[j-1])
                {
                    lcs[i][j]=lcs[i-1][j-1]+1;
                }
                else
                {
					if(lcs[i][j-1]>lcs[i-1][j])
						lcs[i][j]=lcs[i][j-1];
					else
						lcs[i][j]=lcs[i-1][j];
                }
            }
        }
        cout<<lcs[a.length()][b.length()]<<endl;
    }
}

}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
 
using namespace std;
int dp[1002][1002];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
    int n;
    cin>>n;
    string s1,s2;
    while(n--){
        cin>>s1>>s2;
        FOR(i,1,s1.size()+1){
            FOR(j,1,s2.size()+1){
                if(s1[i-1] == s2[j-1])dp[i][j] = dp[i-1][j-1]+1;
                else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[s1.size()][s2.size()] << endl;
    }
  return 0;
}