Go: package main

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

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

using namespace std;

#if 0
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2065462#1
#endif

int n;
string a, b;
string x[10000], y[10000];




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 = 0; i < a.size(); ++i){
      for(int j = 0; j < b.size(); ++j){
        
        x[i] = a;
        y[j] = b;
      }
    }
    */
   //longest[0][0] =0;
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        /*
        if(i <= 0 || j <=0){
          longest[i][j] = 0;
        }
        */
        if(x[i] == y[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 <iostream>
#include <string>

/*int LCS(std::string str1, std::string str2, int i, int j){
  if(str1[i] == str2[j]){
    return 1;
  }
  else{
    return 0;
  }
 }
*/

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

int main(){
  int q;
  std::cin >> q;
  //int ans[q];
  std::string str1, str2;
  for(int z=0; z<q; z++){
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1+1][l2+1];

    for(int i=0; i<=l1; i++){
      DP[i][0] = 0;
    }
    for(int j=0; j<=l2; j++){
      DP[0][j] = 0;
    }

    for(int i=1; i<=l1; i++){
      for(int j=1; j<=l2; j++){
	if(str1[i-1] == str2[j-1]){
	  DP[i][j] = max(DP[i-1][j], DP[i][j-1], DP[i-1][j-1] + 1);
	}
	else{
	  DP[i][j] = max(DP[i-1][j], DP[i][j-1], DP[i-1][j-1]);
      }
    }
    //ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}

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

int dp[1100][1100];

int solve(string s, string t) {
  for (int i = 0; i < 1100; i++) {
    for (int j = 0; j < 1100; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  return dp[s.size()][t.size()];
}

int main() {
  int q;
  cin >> q;
  vector<int> ans;
  for (int i = 0; i < q; i++) {
    string X, Y;
    cin >> X;
    cin >> Y;
    ans.push_back(solve(X, Y));
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  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 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() {
	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 <string>
#include <iostream>
#include <algorithm>
using namespace std;
int q, n, m, dp[1007][1007]; string s, t;
int main() {
	cin >> q;
	while(q--) {
		cin >> s >> t;
		n = s.size(), m = t.size();
		for(int i = 0; i <= n; i++) dp[i][0] = 0;
		for(int j = 0; j <= n; j++) dp[0][j] = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				dp[i][j] = (s[i - 1] == t[i - 1] ? dp[i - 1][j - 1] + 1 : max(dp[i - 1][j], dp[i][j - 1]));
			}
		}
		cout << dp[n][m] << endl;
	}
}

C++: /*
 * Problem Description: Longest Common Subsequence
 * Category: Dynamic Programming
 * Author: Khan
 * Date: 18th October, 2017
 */

#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 maxLength = 0;
    x = ' ' + x;
    y = ' ' + y;
    for (int i = 1; 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]);
            maxLength = max(maxLength, c[i][j]);
        }
    return maxLength;
}

int main(int argc, char const *argv[])
{
    //freopen("sample.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        string x, y;
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }
    return 0;
}

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

using namespace std;

void lcs(string x,string y){
  int m=x.length();
  int n=y.length();
  int maxl=0;
  int c[m+1][n+1];
  for(int i=1;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-1]==y[j-1])
        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]);
    }
  }
  cout << maxl << endl;
}

int main(){
  int q;
  cin >> q;
  for(int i=0;i<q;i++){
    string x,y;
    cin >> x >> y;
    lcs(x,y);
  }
}