Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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=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]==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 <iostream>
#include <cmath>
using namespace std;

int LCS(string str1, string str2)
{
  int n1, n2;
  n1 = (int)(str1.length());
  n2 = (int)(str2.length());
  int dp[n1+1][n2+1];

  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      dp[i][j] = 0;
    }
  }


  int maxi, s, t;
  for(int i = 1; i < n1 + 1; i++) {
    for(int j = 1; j < n2 + 1; j++) {
      if(str1[i-1] != str2[j-1]) {
	dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      }
      else {
	dp[i][j] = dp[i-1][j-1] + 1; //(i-1,j)と(i,j-1)の場合はstr1[i]とstr2[j]が考慮されているので斜め移動のみ
      }
    }
  }
  /*
  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      cout << dp[i][j] << " ";
    }
    cout << "\n";
  }
  */
  maxi = 0;
  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      if(maxi < dp[i][j]) {
	maxi = dp[i][j];
      }
    }
  }
  

  return maxi;
}

int main()
{
  int q;
  cin >> q;
  string str1[q], str2[q];
  for(int i = 0; i < q; i++) {
    cin >> str1[i];
    cin >> str2[i];
  }

  for(int i = 0; i < q; i++) {
    cout << LCS(str1[i], str2[i]) << "\n";
  }

  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#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 i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    for(i = 1 ; i <= m ; i++ ) {
        for(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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        printf("%d\n", lcs(x,y));
    }

    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], i, j;
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for(i = 0; i <= m; i++) c[i][0] = 0;
  for(j = 1; j <= n; j++) c[0][j] = 0;

  for(i = 1; i <= m; i++) {
    for(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][j - 1], c[i - 1][j]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return c[m][n];
}

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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(long long i=0;i<n;i++)
#define P pair<ll,ll>
#define F first
#define S second
#define all(a) (a.begin(),a.end())
long long INF=1000000000000000000;
long long mod=1000000007;
int dp[1010][1010];
int main(void){
    int q;cin>>q;string x,y;
    rep(k,q){
        cin>>x>>y;
        rep(i,1010)rep(j,1010)dp[i][j]=0;
        
        rep(i,x.size())rep(j,y.size()){
            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;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
int main(void){
    int cnt;
    int n;
    std::cin>>n;

    for(int k=0;k<n;k++){
        std::string str1;
        std::string str2;
        std::cin>>str1;
        std::cin>>str2;
        int l1=str1.size();
        int l2=str2.size();
        int A[l1][l2]={};

        for(int i=0;i<l1;i++){
            for(int j=0;j<l2;j++){
                if(i==0 or j==0){
                    if(str1[i]==str2[j]){
                        A[i][j]=1;
                    }else{
                        A[i][j]=0;
                    }
                }else if(str1[i]==str2[j]){
                    A[i][j]=A[i-1][j-1]+1;
                }else{
                    A[i][j]=std::max(A[i][j-1],A[i-1][j]);
                }
                //std::cout<<A[i][j]<<" ";
            }
            //std::cout<<"\n";
        }
        std::cout<<A[l1-1][l2-1]<<"\n";
    }
    return 0;
}

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

int main()
{
	int n;
	string b, c;
	cin >> n;
	for (int i = 0; i < n; i++){
		int a[1010][1010] = {};
		cin >> b >> c;
		for (int x = 1; x <= b.size(); x++){
			for (int y = 1; y <= c.size(); y++){
				if (b[x - 1] == c[y - 1]){
					a[x][y] = a[x - 1][y - 1] + 1;
				}
				else {
					a[x][y] = max(a[x][y - 1], a[x - 1][y]);
				}
			}
		}
		cout << a[b.size()][c.size()] << endl;
	}
	return 0;
}