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++: /*
 * ALDS1_10_C.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

int dp[1001][1001];
bool dpR[1001][1001];

int lcm(char* X, char* Y, int nx, int ny) {
	dp[1][1] = (X[0] == Y[0]);
	for(int i=1; i<=nx; i++) {
		for(int j=1; j<=ny; j++) {
			dp[i][j] = (X[i-1]==Y[j-1]) + dp[i-1][j-1];
			dp[i][j] = std::max(dp[i-1][j], dp[i][j]);
			dp[i][j] = std::max(dp[i][j-1], dp[i][j]);
		}
	}
	return dp[nx][ny];
}

int lcmRec(char* X, char* Y, int nx, int ny) {
	if(nx <=0 || ny <= 0) return 0;
	if(!dpR[nx][ny]) {
		dpR[nx][ny] = 1;
		dp[nx][ny] = lcmRec(X, Y, nx-1, ny-1) + (X[nx-1]==Y[ny-1]);
		dp[nx][ny] = std::max(lcmRec(X, Y, nx-1, ny), dp[nx][ny]);
		dp[nx][ny] = std::max(lcmRec(X, Y, nx, ny-1), dp[nx][ny]);
	}
	return dp[nx][ny];
}

int main() {
	int q;
	scanf("%d", &q);
	char X[1001];
	char Y[1001];
	while(q--) {
		scanf("%s", X);
		scanf("%s", Y);
		int nx = (int)strlen(X);
		int ny = (int)strlen(Y);
		//printf("%d\n", lcm(X, Y, nx, ny));
		printf("%d\n", lcmRec(X, Y, nx, ny));
	}
}

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

const int MAX_LENGTH = 1024;

char A[MAX_LENGTH + 1];
char B[MAX_LENGTH + 1];

int tu[MAX_LENGTH][MAX_LENGTH];

void lcs(int, int);

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> A >> B;
		int lA = strlen(A), lB = strlen(B);
		lcs(lA, lB);
		cout << tu[lA][lB] << endl;
	}

}

void lcs(int lA, int lB)
{
	for (int i = 0; i < lA; ++i)
		tu[0][i] = 0;
	for (int i = 0; i < lB; ++i)
		tu[i][0] = 0;

	for (int i = 1; i <= lA; i++)
	{
		for (int j = 1; j <= lB; j++)
		{
			if (A[i-1] == B[j-1])
				tu[i][j] = tu[i - 1][j - 1] + 1;
			else
				tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]);
		}
	}
}

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

static const int N = 1000;

int Ics(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 ( int i = 0; i<n; i++ ) {
		cin >> s1 >> s2;
		cout << Ics(s1, s2) << endl;
	}
	return 0;
}

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

int max(int a,int b){
  if(a<b)return b;
  else return a;
}

int main(void){
  char x[1005]={0};
  char y[1005]={0};
  int set=0;
  int dp[1005][1005]={0};
  /*dp[i][j]は、xのi文字目とyのj文字目での最長部分列*/
  scanf("%d",&set);
  int p=0,i=0,j=0;;
  for(p=0;p<set;p++){
    scanf("%s%s",x,y);
    int xl,yl;
    xl=strlen(x);yl=strlen(y);
    int ma,mb,mc;
    for(i=1;i<xl+1;i++){
      for(j=1;j<yl+1;j++){
	ma=dp[i][j-1];
	mb=dp[i-1][j];
	mc=dp[i-1][j-1];
	if(x[i-1]==y[j-1])mc++;
	//dp[i][j]= ma<mb ? (mb<mc ? mc : mb) : ma;
	dp[i][j]= max(ma,max(mb,mc));
      }
    }
    printf("%d\n",dp[xl][yl]);
  }
  return 0;
}

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

string X;
string Y;

int calc(int x_s, int x, int y, int length){
    if (X[x] == '\0') return length;
    int y_ = Y.find(X[x], y);
    if (y_ == string::npos) {
        if ((Y[y+1] == '\0')) {
            return max(length, calc(x_s + 1, x_s + 1, 0, 0));
        }
        return max(length, calc(x_s, x, y + 1, length));
    }
    return calc(x_s, x + 1, y_ + 1, length + 1);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        cout << calc(0, 0, 0, 0) << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <utility>
using namespace std;
#define MAXQ 150
#define MAXL 1000

int main(){
    int n; cin >> n;
    pair<string, string> pairs[MAXQ];
    for(int i=0; i<n; ++i){
        cin >> pairs[i].first >> pairs[i].second;
    }
    
    for(int i=0; i<n; ++i){
        string str1, str2;
        str1 = pairs[i].first;
        str2 = pairs[i].second;
        int dp[MAXL][MAXL];
        for(int r=0; r<=str1.size(); ++r){
            dp[r][0] = 0;
        }
        for(int c=0; c<=str2.size(); ++c){
            dp[0][c] = 0;
        }
        for(int r=1; r<=str1.size(); ++r){
            for(int c=1; c<=str2.size(); ++c){
                int d = 0;
                if (str1[r] == str2[c]) d++;
                dp[r][c] = max(dp[r-1][c-1]+d, max(dp[r-1][c], dp[r][c-1]));
            }
        }
        cout << dp[str1.size()][str2.size()] << endl;
    }
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    //dp[i][j] Xのi文字目までとYのj文字目までのLCS
    int q;
    cin >> q;
    rep(i, q) {
        string X, Y;
        cin >> X >> Y;
        int m = X.size(), n = Y.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        rep(j, m) {
            rep(k, n) {
                if (X[j] == Y[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[m][n] << "\n";
    }
}

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

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

        int DP[1024][1024] = {};

        for(int i=0;i<=(int)X.size();++i){
            for(int j=0;j<=(int)Y.size();++j){
                if(X[i] == Y[j]){ // X???i???????????¨Y???j??????????????????????????????
                    DP[i][j] = DP[i-1][j-1] + 1;
                } else {
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
                }
            }
        }
        cout << DP[(int)X.size()][(int)Y.size()] << endl;
    }
}