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 <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
 
using namespace std;
int dp[1002][1002];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
    int n;
    cin>>n;
    string s1,s2;
    while(n--){
        cin>>s1>>s2;
        FOR(i,1,s1.size()+1){
            FOR(j,1,s2.size()+1){
                if(s1[i-1] == s2[j-1])dp[i][j] = dp[i-1][j-1]+1;
                else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[s1.size()][s2.size()] << endl;
    }
  return 0;
}

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

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;


int memo[1001][1001];
bool checked[1001][1001];
int lcs(string s, string t, int si, int ti) {
  
  if( si == -1 || ti == -1 ) {
    return 0;
  }
  if(checked[si][ti] == true) {
    return memo[si][ti];
  }
  checked[si][ti] = true;

  int res = 0;
  if( s[si] == t[ti] ) {
    res = lcs(s, t, si-1, ti-1) + 1;
  } else {
    res = max( lcs(s, t, si-1, ti), lcs(s, t, si, ti-1) );
  }

  return memo[si][ti] = res;

}

int solve() {

  string s, t;
  cin >> s >> t;

  // initialize memo 
  fill(checked[0], checked[1001], false);
  return lcs(s, t, s.size()-1, t.size()-1);


  // dp[i][j] := s_1,...s_i と t_1,...t_j に対するLCSの長さ
  // s[i] == t[j] なら dp[i-1][j-1] + 1
  // s[i] != t[j] なら max( dp[i-1][j], dp[i][j-1] )
  // int dp[s.size()+1][t.size()+1];
  // fill(dp[0], dp[s.size()+1], 0);
  // for(int i = 0; i < s.size(); i++) {
  //   for(int j = 0; j < t.size(); j++) {
  //     if( s[i] == t[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]);
  //     }
  //   }
  // }

  // return dp[s.size()][t.size()];



}


int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  for(int i = 0; i < N; i++) {
    cout << solve() << "\n";
  }


  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;
typedef long long int ll;
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 i = 1; i <= N; i++) C[0][i] = 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;
	}
}

C++: #include <iostream>

using namespace std;
const int MAX = 1001;

short max(short a,short b) {
	if (a > b) {
		return a;
	} else {
		return b;
	}
}
short solve(string str1,string str2) {
	int len1 = str1.length();
	int len2 = str2.length();
	str1 = " " + str1;
	str2 = " " + str2;
	short LCS[MAX][MAX];
	for (int i=0;i<=len1;i++) {
		LCS[i][0] = 0;
	}
	for (int i=0;i<=len2;i++) {
		LCS[0][i] = 0;
	}
	int max1 = 0;

	for (int i=1;i<=len1;i++) {
		for (int j = 1;j<=len2;j++) {
			if (str1[i] == str2[j]) {
				LCS[i][j] = LCS[i-1][j-1] + 1;
			} else {
				LCS[i][j] = max(LCS[i-1][j],LCS[i][j-1]);
			}
			max1 = max(LCS[i][j],max1);
		}
	}
	return max1;
}

int main() {
	int n;
	cin >> n;
	for (int i=0;i<n;i++) {
		string str1,str2;
		cin >> str1 >> str2;
		cout << solve(str1,str2) << endl;
	}
	return 0;

}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;cin>>q;
  FOR(i,0,q){
    string x, y;
    cin >> x >> y;
    int n = x.length();
    int m = y.length();
    int dp[n+1][m+1];
    CLR(dp);
    FOR(i,0,n){
      FOR(j,0,m){
        if(x[i]==y[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][j+1]);
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
      }
    }
    cout<<dp[n][m]<<endl;
  }
}

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

int main(){
  int n;
  std::string x,y;
  std::cin>>n;
  int a[1001][1001];
  int b[n];

  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1001;j++){
      for(int k=0;k<1001;k++){
	a[j][k]=0;
      }
    }
    for(int j=1;j<=x.size();j++){
      for(int k=1;k<=y.size();k++){
	if(x[j-1]==y[k-1]){
	  a[j][k]=a[j-1][k-1]+1;
	}else{
	  if(a[j-1][k]>a[j][k-1]){
	    a[j][k]=a[j-1][k];
	  }else{
	    a[j][k]=a[j][k-1];
	  }
	}
      }
    }
    b[i]=a[x.size()][y.size()];
  }

  for(int i=0;i<n;i++){
  std::cout<<b[i]<<"\n";
  }

  return 0;
}

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


int lcs(string a, string b) {
	int dp[1009][1009];

	for (int i = 0; i <= a.size(); i++) {
		for (int j = 0; j <= b.size(); j++) {
			if (i == 0 || j == 0) dp[i][j] = 0;
			else if (a[i - 1] == b[j - 1])
				dp[i][j] = max(max(dp[i - 1][j - 1] + 1, dp[i - 1][j]), dp[i][j - 1]);
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
		}
	}
	return dp[a.size()][b.size()];
}

int main() {
	int q;
	cin >> q;
	getchar();
	while (q--) {
		string a, b;
		getline(cin, a);
		getline(cin, b);
		/*cout << "=======" << a << "=======" << endl;
		cout << "=======" << b << "=======" << endl;*/
		cout << lcs(a, b) << endl;
	}
	return 0;
}