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;

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

  vector<int> anss;
  for (int i = 0; i < n; i++)
  {
    string s,t;
    cin >> s >> t;

    int lens = s.size();
    int lent = t.size();
    int dp[lens][lent];
    bool flg = false;
    for (int j = 0; j < lens; j++)
    {
      if(t[0]==s[j]) flg = true;
      if(flg) dp[j][0] = 1;
      else dp[j][0] = 0;
    }
    flg = false;
    for (int j = 0; j < lent; j++)
    {
      if(s[0]==t[j]) flg = true;
      if(flg) dp[0][j] = 1;
      else dp[0][j] = 0;
    }

    for (int j = 1; j < lens; j++)
    {
      for (int k = 1; k < lent; k++)
      {
        if(s[j]==t[k]) dp[j][k] = dp[j-1][k-1]+1;
        else dp[j][k] = max(dp[j-1][k],dp[j][k-1]);
      }      
    }
    anss.push_back(dp[lens-1][lent-1]);
  }
  for(int ans:anss) cout << ans << endl;
}

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

int main() {
  int q; cin >> q;
  string A, B;
  for (int i=0; i<q; i++) {
    cin >> A >> B;
    vector<vector<int>> LCS(A.length()+1, vector<int>(B.length()+1,0));
    int ans=0;
    for (int j=1; j<=A.length(); j++) {
      for (int k=1; k<=B.length(); k++) {
        if(A[j]==B[k]) LCS[j][k]=LCS[j-1][k-1]+1;
        else LCS[j][k]=max(LCS[j-1][k],LCS[j][k-1]);
        ans = max(ans, LCS[j][k]);
      }
    }
    cout << ans << "\n";
  }
}

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] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return maxl;
}

int main() {
  string s1, s2;
  int q;
  cin >> q;

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

  return 0;
}

C++: #include <stdio.h>

int judge(char A[], char B[]){
  int a=0, b=0, sw=0, ans=0;
  int i, j;
  int C[1001][1001];
  
  while(sw == 0){
    if(A[a] != EOF)
      a++;
    else
      sw = 1;
  }
  
  sw = 0;
  
  while(sw == 0){
    if(B[b] != EOF)
      b++;
    else
      sw = 1;
  }
  
  for(i=1; i<a+1; i++)
    C[i][0] = 0;
  for(i=1; i<b+1; i++)
    C[0][i] = 0;
  
  for(i=1; i<a+1; i++){
    for(j=1; j<b+1; j++){
      if(A[i-1] == B[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(ans < C[i][j])
        ans = C[i][j];
    }
  }
  
  return ans;
}

int main(){
  char A[1000], B[1000];
  int n, i;
  
  scanf("%d", &n);
  
  for(i=0; i<n; i++){
    scanf("%s", A);
    scanf("%s", B);
    printf("%d\n", judge(A, B));
  }
  
  return 0;
}

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

using namespace std;

int main() {
	int n;	
	int c[1001][1001];
	string str1, str2;

	cin >> n;

	for (int k = 0; k < n; k++) {
		int Max = 0;
		cin >> str1 >> str2;
		
		int m = str1.size();
		int n = str2.size();


		str1 = ' ' + str1;
		str2 = ' ' + str2;

		//cout << str1 << str2 << endl;


		for (int i = 0; i <= m; i++) {
			for (int j = 0; j <= n; j++) {
				if (i == 0 || j == 0) c[i][j] = 0;
				else if (str1[i] == str2[j]) c[i][j] = c[i - 1][j - 1] + 1;
				else c[i][j] = max(c[i][j - 1], c[i - 1][j]);

				Max = max(Max, c[i][j]);

				//cout << c[i][j] << " ";

			}

			//cout << endl;

		}

		cout << Max << endl;
	}

	return 0;

}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;

int dp[1010][1010];
string x, y;

int solve(int a, int b) {
    if (dp[a][b] >= 0) {
        return dp[a][b];
    }
    
    if (a * b == 0) {
        dp[a][b] = 0;
        return 0;
    }
    
    if (char(x[a - 1]) == char(y[b - 1])) dp[a][b] = solve(a - 1, b - 1) + 1;
    else dp[a][b] = solve(a - 1, b - 1);
    
    dp[a][b] = max(dp[a][b], max(solve(a - 1, b), solve(a, b - 1)));
    
    return dp[a][b];
}

int main(void){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        
        for (int i = 0; i <= x.size(); i++) {
            for (int j = 0; j <= y.size(); j++) dp[i][j] = -1;
        }
        dp[0][0] = 0;
        
        cout << solve(x.size(), y.size()) << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespase 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 = 0; i <= m; i++ ) c[i][0] = 0;
    for ( int j = 1; 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 ( i = 0; i < n; i++ ){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << sndl;
    }
    
    return 0;
    
}