Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i][j-1], dp[i-1][j])
			}
		}
	}
	return dp[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

C++: //
// Created by 李青坪 on 2018/10/29.
//

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>

#define MAXN 150
#define MAXLEN 1000
using namespace std;

int c[MAXLEN+1][MAXLEN+1];

int getSubsequence(string x, string y){
    int i = 0, j = 0;
    while (i <= x.length()){
        while (j <= y.length()){
            if (i == 0 || j ==0){
                c[i][j] = 0;
            } else if (x[i-1] == y[j-1]){
                c[i][j] = c[i-1][j-1] + 1;
            } else if (x[i-1] != y[j-1]){
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
            ++j;
        }
        ++i;
        j = 0;
    }
    return c[x.length()][y.length()];
}

int main(){
    int n;
    scanf("%d", &n);
    string x, y;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        printf("%d\n", getSubsequence(x, y));
    }

}
/*
3
abcbdab
bdcaba
abc
abc
abc
bc
 */

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 = 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 << lcs(s1, s2) << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <string>
#pragma warning(disable : 4996)

#define MAX_L 1000

int DP[MAX_L+1][MAX_L+1];

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

int main(void) {
	int n;
	scanf("%d", &n);
	char X[MAX_L + 1];
	char Y[MAX_L + 1];
	for (int i = 0; i < n; ++i) {
		scanf("%s", &X[0]);
		scanf("%s", &Y[0]);
		int xl = strlen(X);
		int yl = strlen(Y);

		for (int j = 0; j < yl; ++j) {
			for (int i = 0; i < xl; ++i) {
				if (X[i] == Y[j]) {
					DP[j + 1][i + 1] = DP[j][i] + 1;
				}
				else {
					DP[j + 1][i + 1] = max(DP[j][i + 1], DP[j + 1][i]);
				}
			}
		}
		printf("%d\n", DP[yl][xl]);

	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#define N 1000
using namespace std;

int ans[N+1][N+1];

int deal(string s1,string s2){
    int maxl=-1;

    for(int i=0;i<s1.length();i++){
        ans[i][0]=0;
    }
    for(int j=0;j<s2.length();j++){
        ans[0][j]=0;
    }
    for(int i=1;i<s1.length();i++){
        for(int j=1;j<s2.length();j++){
            if(s1[i]==s2[j]){
                ans[i][j]=ans[i-1][j-1]+1;
            }else{
                ans[i][j]=max(ans[i-1][j],ans[i][j-1]);
            }
            maxl=max(maxl,ans[i][j]);
        }
    }
    return maxl;
}

int main(){
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        s1=" "+s1;
        s2=" "+s2;
        cout<<deal(s1,s2)<<endl;
    }
    return 0;
}

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

#define MAX 1000

int lcs(string x, string y) {
  int c[MAX + 1][MAX + 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 <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int q;
string x, y;
int dp[1111][1111];

void solve() {
   for (int i = 0; i < x.size(); i++) {
      for (int j = 0; j < y.size(); j++) {
         if (x[i] == y[j]) {
            dp[i+1][j+1] = dp[i][j] + 1;
         } else {
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
         }
      }
   }
   cout << dp[x.size()][y.size()] << endl;
}

int main() {
   cin >> q;
   while (q--) {
      cin >> x >> y;
      solve();
   }

   return 0;
}

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 maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;

    for (int i = 1; i <= X.size(); i++) {
        for (int j = 1; j <= Y.size(); 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 << lcs(s1, s2) << endl;
    }
    return 0;
}