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 f() {
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	dp := make([][]int, lenS+1)
	for i:=0; i<lenS+1; i++ {
		dp[i] = make([]int, lenT+1)
	}
	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])
}

func main(){
	var n int 
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		f()
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define For(I,A,B) for(int I=(A); I < (B); ++I)
int c[1005][1005];
int main()
{
int t;
string p;
cin>>t;
getline(cin,p);
while(t--){

int x,y,z,k=0,l,i,j;
string a,b,q,r;

 For(i,0,1005)
 For(j,0,1005)
 c[i][j]=0;

 getline(cin,a);
 getline(cin,b);
 x=a.length();
 y=b.length();
 j=1;
  For(i,0,x) q[j++]=a[i];
  j=1;
  For(i,0,y) r[j++]=b[i];
 For(i,1,x+1)
 For(j,1,y+1)
 {
     if(q[i]==r[j]) c[i][j]=c[i-1][j-1]+1;
     else  c[i][j]=max(c[i-1][j],c[i][j-1]);
 }
    cout<<c[x][y]<<endl;

}

    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>

int dp[150+1][150+1];

int lcsSub(std::string X, std::string Y, int i, int j){
	if (i==-1 || j==-1){
		return 0;
	}
	else{
		if(X[i]==Y[j]){
			if (dp[i][j] < 0){
				dp[i][j] = lcsSub(X, Y, i-1, j-1);
			}
			return dp[i][j]+1;
		}
		else{
			int up, left;
			if (dp[i+1][j] < 0){
				dp[i+1][j] = lcsSub(X, Y, i, j-1);
			}
			left = dp[i+1][j];
			if (dp[i][j+1] < 0){
				dp[i][j+1] = lcsSub(X, Y, i-1, j);
			}
			up = dp[i][j+1];
			
			return std::max(up, left);
		}
	}
}

int lcs(std::string X, std::string Y){
	int m,n,i,j;
	m = X.length();
	n = Y.length();
	for(i=0;i<m+1;i++){
		for(j=0;j<n+1;j++){
			dp[i][j] = -1;
		}
	}
	return lcsSub(X, Y, m-1, n-1);
}
int main(void){
	int i,q,n,m;
	std::string str1, str2;
	std::cin >> q;
	for (i=0;i<q;i++){
		std::cin >> str1;
		std::cin >> str2;
		std::cout << lcs(str1, str2) << std::endl;
	}
	return 0;
}

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

int main() {
	int setN;
	cin >> setN;
	string A,B;

	for (int i=0;i<setN;i++) {
		cin >> A;
		cin >> B;
		int L[2000][2000] ={};

		for(int j=1;j<A.length()+1;j++) {
			for(int l=1;l<B.length()+1;l++) {
				if(A[j-1] == B[l-1]) {
					L[j][l] = 1+L[j-1][l-1];

				} else {
					L[j][l] = max(L[j-1][l],L[j][l-1]);
				}
				
			}
		}
		/*
		for(int j=0;j<A.length()+1;j++) {
			cout << endl;
			for(int l=0;l<B.length()+1;l++) {
				cout << L[j][l] << " ";
				
			}
		}
		*/

		cout << L[A.length()][B.length()] << endl;


	}
}

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

int lcs(string str1, string str2) {
	int n1 = str1.size();
	int n2 = str2.size();
	int A[n1 + 1][n2 + 1];
	for (int i = 0; i < n1; i++) A[i][0] = 0;
	for (int j = 0; j < n1; j++) A[0][j] = 0;

	for (int i = 1; i <= n1; i++) {
		for (int j = 1; j <= n2; j++) { 
			if (str1[i - 1] == str2[j - 1]) {
				A[i][j] = A[i - 1][j - 1] + 1; //?????´??????+1????????¨???????????¨???????????\??????i-1, j-1????????´??°???i,j?????????????????????+1????????¨???????????¨i-1,j??¨?????§?????????????????¶??????
			} else {
				A[i][j] = max(A[i - 1][j], A[i][j - 1]); //?????´?????????????????§i,j??????????????? i-1,j??¨i,j-1
			}
		}
	}
	return A[n1][n2];
}

int main() {
	int n;
	string str1, str2;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str1 >> str2;
		cout << lcs(str1, str2) << endl;
	}
	return 0;
}

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

static const int N = 1000;

int lcs(string X,string Y){
    int c[N+1][N+1];    //c[i][k]???Xi??¨Yk???LCS??¨??????????????????
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X; // X[0]?????????????????\
    Y = ' ' + Y;
    for(int i=1;i<=m;i++) c[i][0] = 0;
    for(int k=1;k<=n;k++) c[0][k] = 0;
    c[0][0] = 0;

    for(int i=1;i<=m;i++){
        for(int k=1;k<=n;k++){
            if(X[i]==Y[k]){
                c[i][k] = c[i-1][k-1] + 1;
            } else {
                c[i][k] = max(c[i-1][k],c[i][k-1]);
            }
            maxl = max(maxl,c[i][k]);
        }
    }

    return maxl;
}

int main(){
    
}

C++: #include <iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <string>

#define MAX 10000

using namespace std;

int n;
int LCS[MAX][MAX];

void solve(string a, string b){
    int m = a.size();
    int n = b.size();
    int maxl = 0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            LCS[i][j] = 0;
        }
    }
    bool tmp_ok = false;
    for(int i=0; i<n; i++){
        if(a[0] == b[i] || tmp_ok){
            LCS[i][0] = 1;
            maxl = 1;
            tmp_ok = true;
        }
    }
    tmp_ok = false;
    for(int i=0; i<m; i++){
        if(a[i] == b[0] || tmp_ok){
            LCS[0][i] = 1;
            maxl = 1;
            tmp_ok = true;
        }
    }
    for(int i=1; i<n; i++){
        for(int j=1; j<m; j++){
            if(a[j] == b[i]){
                LCS[i][j] = LCS[i-1][j-1]+1;
            }
            else if(LCS[i-1][j]>=LCS[i][j-1]){
                LCS[i][j] = LCS[i-1][j];
            }
            else LCS[i][j] = LCS[i][j-1];
            maxl = max(maxl, LCS[i][j]);
        }
    }
    
    cout << maxl << endl;
}

int main(int argc, const char * argv[]) {
    // input from txt
    
    /////////
    //write//
    /////////
    
    cin >> n;
    for(int i=0; i<n; i++){
        string str_a;
        string str_b;
        cin >> str_a >> str_b;
        solve(str_a, str_b);
    }
    
    return 0;
}

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

int main(){
  int n;
  char x[1000],y[1000];
  int dp[1001][1001];
  //load n;
  cin >>n;
  for(int i=0;i<n;i++){
    scanf("%s",x);
    scanf("%s",y);
    for(int j=0;j<strlen(x);j++){
      for(int k=0;k<strlen(y);k++){
	if(x[j]==y[k]){
	  dp[j+1][k+1]=dp[j][k]+1;
	}

	else{
	  if(dp[j][k+1]>dp[j+1][k]){
	    dp[j+1][k+1]=dp[j][k+1];
	  }

	  else{
	    dp[j+1][k+1]=dp[j+1][k+1];
	  }


	}
      }
    }

  }
  cout <<dp[strlen(x)][strlen(y)]<<endl;

  return 0;
}