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(){
	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<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;

const int MAX = 1000;
static int n, m;
static char S[150][MAX], T[150][MAX];
static int ans[150][MAX];
void solve(int in)
{
	int count_max = 0;
	for (int i = n - 1; i >= 0; i--) {
		count_max = 0;
		for (int j = n - 1; j >= 0; j--) {
			int count = 0;
			int tem_i = i, tem_j = j;
			//?????´??????????????????????????????????????´????????´????????¢?´¢??????
			if (S[in][tem_i] == T[in][tem_j] && S[in][tem_i + 1] == T[in][tem_j + 1] && tem_i + 1 < n && tem_j + 1 < m)
				break;
			while (S[in][tem_i] == T[in][tem_j]&& tem_i>=0 && tem_j>=0 ) {
				count++;
				tem_i--;
				tem_j--;
			}
			if (count_max < count) {
				count_max = count;
			}
		}
		ans[in][i] = count_max;
		//???????????§??§i??????????????§?????¨??????????±???????.
	}
}
int main()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> S[i];
		cin >> T[i];
		n = strlen(S[i]);
		m = strlen(T[i]);
		solve(i);
	}

	for (int i = 0; i < q; i++)
	cout << *max_element(&ans[i][0], &ans[i][n]) << endl;

	return 0;
}

C++: #include <iostream>
#include <string>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

int q;
int dp[1000][1000];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( { ret, rec( i+1, j ), rec( i, j+1 ) } );

  return ret;
}

int main()
{
  scanf( "%d", &q );
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

C++: #include<stdio.h>
#include<string.h>
????????
int LCS(char *X, char *Y){
????int i,j,m,n,c[1001][1001];
????m = strlen(X);
????n = strlen(Y);
????????????
????for(i=0; i<m; i++){
????????c[i][0] = 0;
????}
????for(j=0; j<n; j++){
????????c[0][j] = 0;
????}
????????
????for(i=1;i<m+1;i++){
????????for(j=1;j<n+1;j++){
????????????if (X[i-1]==Y[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];
????????}
????}
????return c[i-1][j-1];
}
????????
int main(){
??}
????return 0;
}

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

int lcs(string &X, string &Y) {
  int dp[1000 + 1][1000 + 1];
  int max = 0;
  int m = X.size();
  int n = Y.size();
  X = ' ' + X;
  Y = ' ' + Y;

  for (int i = 1; i <= m; i++)
    dp[i][0] = 0;

  for (int i = 1; i <= n; i++)
    dp[0][i] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
	dp[i][j] = dp[i-1][j - 1] + 1;
      else if (dp[i - 1][j] >= dp[i][j - 1])
	dp[i][j] = dp[i - 1][j];
      else
	dp[i][j] = dp[i][j - 1];

      if(max < dp[i][j])
	max = dp[i][j];
    }
  }

  return max;
}

int main() {
  int N = 0;
  cin >> N;

  string X, Y;
  for (int i = 0; i < N; i++) {
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }

  return 0;
}

C++: #if 0
?????????X[0]???????????????????????\???????????¨??§???X[1],Y[1]???????????????????????????????????????
#endif

#include <iostream>
#include <string>
using namespace std;

int main(){
	int q;
	cin >> q;
	for(int a=0;a<q;++a){
		string X,Y;
		int L[1000][1000];
		int MAX;
	cin >> X >> Y;
	for(int i=0;i<1000;++i) //???????????\????????????L????????????
		for(int j=0;j<1000;++j)
			L[i][j]=0;

	X+=" "; 
	Y+=" ";
	
	cin >> q;
	for(int j=1;j<X.length()+1;++j)
		for(int k=1;k<Y.length()+1;++k){
			if(X[j]==Y[k])
				L[j][k]=1+L[j-1][k-1];
			else
				L[j][k]=max(L[j-1][k],L[j][k-1]);
			MAX = max(MAX,L[j][k]);
		}
	cout << MAX << endl;
	}
}

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

using namespace std;



int n;
string a, b;



int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    
   
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        if(j == 0 || i == 0){
          longest[i][j] = 0;
        }
        else if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

C++: #include<bits/stdc++.h>
#define N 1005
using namespace std;
int n,dp[N][N],xlen,ylen;
string x,y;

int main(){
  cin>>n;
  while(n--){
    cin>>x>>y;
    xlen=x.size();
    ylen=y.size();
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=xlen;i++){
      for(int j=1;j<=ylen;j++){
	if(x[i-1]==y[j-1])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
	dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));
      }
    }
    cout<<dp[xlen][ylen]<<endl;
  }
  return 0;
}

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

int longestCommonSubsequence(string X, string Y) {
	int DP_memory[1000+1][1000+1]; //0~length??????
	int sizeX = X.length(); //0????????????????????????????????????????????????????????§???
	int sizeY = Y.length(); //???????????£???????????????
	X = " " + X;
	Y = " " + Y;
	for (int i = 0; i <= sizeX; i++)
		DP_memory[i][0] = 0;
	for (int i = 0; i <= sizeY; i++)
		DP_memory[0][i] = 0;

	for (int i = 1; i <= sizeX; i++) {
		for (int j = 1; j <= sizeY; j++) {
			if (X[i] == Y[j])
				DP_memory[i][j] = DP_memory[i - 1][j - 1] + 1;
			else if (DP_memory[i - 1][j] >= DP_memory[i][j - 1])
				DP_memory[i][j] = DP_memory[i - 1][j];
			else
				DP_memory[i][j] = DP_memory[i][j - 1];
		}
	}
	return DP_memory[sizeX][sizeY];
}

int main() {
	string lcsX[1000];
	string lcsY[1000];
	int setNum;

	cin >> setNum;
	for (int i = 0; i < setNum; i++) {
		cin >> lcsX[i];
		cin >> lcsY[i];

		int lcs = longestCommonSubsequence(lcsX[i], lcsY[i]);
		cout << lcs << endl;
	}
	return 0;
}