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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <functional>
#include <climits>

using namespace std;
int c[1001][1001];

int main() {
	int n;
	cin >> n;
	for (size_t r = 0; r < n; r++)
	{
		string s, t;
		cin >> s >> t;
		int p = s.size();
		int q = t.size();
		for (size_t i = 1; i < p+1; i++)
		{
			for (size_t j = 1; j < q+1; j++)
			{
				if(s[i-1] == t[j-1]) {
					c[i][j] = c[i-1][j-1] + 1;
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1]);
				}
			}
			
		}
		cout << c[p][q] << endl;
	}
	
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0;
  int dp[1010][1010];
  char A[1500],B[1500];
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
 
    for(int i=0;i<strlen(A);i++){
      for(int j=0;j<strlen(B);j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strlen(A)][strlen(B)]);
    AC++;
  }
  return 0;
}

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

string a,b;
int A[1002][1002];

int lcs () {
    int ath = a.length(), bth = b.length();
    int i, j;
    for ( i = 0; i < ath; i++ ) A[i][0] = 0;
    for ( j = 0; j < bth; j++ ) A[0][j] = 0;

    for ( i = 1; i <= ath; i++ ) {
        for ( j = 1; j <= bth; j++ ) {
            if ( a[i - 1] == b[j - 1] ) A[i][j] = A[i-1][j-1] + 1;
            else if ( A[i][j-1] > A[i-1][j] ) A[i][j] = A[i][j-1];
            else A[i][j] = A[i-1][j];
        }
    }

    return A[i-1][j-1];
}

int main()
{
    int n;
    cin >> n;
    while ( n-- ) {
        cin >> a >> b;
        cout << lcs() << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MAX 1001

static int dp[MAX][MAX];

int main() {
	int q; cin >> q;
	while (q--) {
		memset(dp, 0, size(dp));
		string X, Y; cin >> X >> Y;
		for (int i = 1; i <= X.size(); i++) {
			for (int j = 1; j <= Y.size(); j++) {
				if (X[i - 1] == Y[j - 1]) {
					//cout << i << j << X[i - 1] << endl;
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(
						dp[i - 1][j],
						dp[i][j - 1]
					);
				}
			}
		}
		//for (int i = 0; i <= Y.size(); i++) {
		//	for (int j = 0; j <= X.size(); j++) {
		//		cout << dp[j][i] << " ";
		//	}
		//	cout << endl;
		//}
		cout << dp[X.size()][Y.size()] << endl;

	}

	return 0;
}

C++: // ALDS1_10_C.cpp
// Dynamic Programming - Longest Common Subsequence

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int LCS(string s, string t)
{
    int res = 0;
    if (s.empty() || t.empty())
        return res;
    int s_len = s.size();
    int t_len = t.size();
    int len = min(s_len, t_len);
//    int lcs[s_len][t_len];
    vector<vector<int>> lcs(s_len, vector<int>(t_len));
    bool found = false;
    for (int i = 0; i < s_len; i++) {
        if (s[i] == t[0] || found) {
            lcs[i][0] = 1;
            found = true;
        }
    }
    found = false;
    for (int j = 0; j < t_len; j++) {
        if (s[0] == t[j] || found) {
            lcs[0][j] = 1;
            found = true;
        }
    }
    for (int i = 1; i < s_len; i++) {
        for (int j = 1; j < t_len; j++) {
            if (s[i] == t[j]) {
                lcs[i][j] = 1 + lcs[i-1][j-1];
            }
            else {
                lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1]);
            }
        }
    }
    return lcs[s_len - 1][t_len - 1];
}

int main()
{
    int n;
    cin >> n;
    string s, t;
    while (n--) {
        cin >> s >> t;
        int res = LCS(s, t);
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+10;j++){
            for(int k = 0;k < X.length()+10;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k] == X[j]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
return 0;
}

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

int main(){
  int q, cnt, max=0;
  string str1, str2;
  char ch1, ch2;
  cin >> q;
  for(int i=0;i<q;i++){
    cin >> str1 >> str2;
    //cout << "cin >> str1 >> str2" << endl;
    for(int j=0;j<(int)str1.size();j++){
      for(int k=0;k<(int)str2.size();k++){
	//cout << "j = " << j << ", k = " << k << endl;
        cnt = 0;
	int l = 0;
	while(1){
	      //str1[j+l] || str2[k+l] || str1[j+l] == str2[k+l]){
	  ch1 = str1[j+l];
	  //  cout << ch1 << " ";
	  ch2 = str2[k+l];
	  //  cout << ch2 <<endl;
	  if(ch1 == ch2 && ch1 != '\0'){
     	    cnt++;
	    l++;
	    //	    cout << "一致" << endl;
	  }else break;
	}
	if(cnt > max) max = cnt;
      }
    }
    cout << max << endl;
  }
}