Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

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;
}

C++: //
// Created by miyoshi on 2019/05/23.
//

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

int main() {
    int number_of_pair;
    string X, Y;

    cin >> number_of_pair;

    for (int i=0; i < number_of_pair; i++){
        cin >> X;
        cin >> Y;
        cout << lcs(X, Y) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>

using namespace std;

int main(){

  int n;
  cin >> n;

  string s[n],t[n];

  for(int i=0;i<n;i++){
    cin >> s[i] >> t[i];
  }

  int a[n];

  for(int k=0;k<n;k++){
    int dp[s[k].size()+1][t[k].size()+1];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < s[k].size(); ++i) {
      for (int j = 0; j < t[k].size(); ++j) {
        if (s[k][i] == t[k][j]) dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);
      }
    }
    a[k]=dp[s[k].size()][t[k].size()];
  }

  for(int i=0;i<n;i++)cout << a[i] << endl;

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int dp[1010][1010];

int main() {
    int q;
    cin>>q;
    while(q--){
        string s,t;
        cin>>s>>t;
        int n=s.size(),m=t.size();
        rep(i,1010){
            rep(j,1010){
                dp[i][j]=0;
            }
        }
        rep(i,n){
            rep(j,m){
                if(s[i]==t[j]) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i+1][j]);
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j+1]);
            }
        }

        cout<<dp[n][m]<<endl;
    }

    return 0;
}