Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = " " + s1 , " " + s2
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)x.size())
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vi  = vector<int>;
constexpr int INF = 1001001001001001001LL;
/*
<NAME> 最長共通部分列 (Longest Common Subsequence)
<TRIGGER> LCS
<TAG> DP, etc
<*NOTE>
文字列の最長共通部分列を求める。$O(NM)$
<*CODE> */
int lcs(string a, string b) {
    int n = sz(a), m = sz(b);
    vvi dp(n + 1, vi(m + 1, 0));
    rep(i, 0, n) {
        rep(j, 0, m) {
            if (a[i] == b[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[n][m];
}

// hash : [59, 176, 206, 65, 188, 72, 103, 19]]

signed main() {
    int Q;
    cin >> Q;
    while (Q--) {
        string S, T;
        cin >> S >> T;
        cout << lcs(S, T) << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 1000
int dp[MAX + 1][MAX + 1];
int LCS(string STR1, string STR2)
{
	int length1 = STR1.size();
	int length2 = STR2.size();
	//???????????¨????????????????????????
	STR1 = ' ' + STR1;
	STR2 = ' ' + STR2;
	for (int i = 0; i < length1; i++)
	{
		for (int l = 0; l < length2; l++)dp[i][l] = 0;
	}
	for (int i = 1; i <= length1; i++)
	{
		for (int l = 1; l <= length2; l++)
		{
			if (STR1[i] == STR2[l])dp[i][l] =dp[i][l-1]+1;
			else
			{
				dp[i][l]=max(dp[i - 1][l], dp[i][l - 1]);
			}
		}
	}
	return dp[length1][length2];
}
int main()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		string str1, str2;
		cin >> str1 >> str2;
		cout << LCS(str1, str2) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
char map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	if(x < 0 || y < 0) return 0;
	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			return findMaxLength(x-1,y-1) +1;
		}else{
			return max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
	}else return map[x][y];
}

int main(){
	
	scanf("%d",&q);

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<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;
  C[0][0] = 0
  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]);
    }
  }javascript:void(0)

  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 <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define nyan "(=^･ω･^=)"
using namespace std;

const int mod = 1e9 + 7;
const long long INF = 1LL<<60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//using ll = long long;
//using P = pair<int,int>;

int main(){
    int q;
    cin >> q;
    
    rep(i, q){
        string x, y;
        cin >> x >> y;
        
        int n = x.size();
        int m = y.size();
        
        // dp[i][j] : sのi番目, tのj番目までに対する 
        // longest common subsequenceの長さ
        vector<vector<int> > dp(n+1, vector<int>(m+1));
        
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; 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[n][m] << endl;
    }
}

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
 int point;

  for(int i=0;i<n;i++){
    ans[i]=0;
  }

  for(int m=0;m<n;m++){
  cin>>X>>Y;
 int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=0;
}
  }

 
 

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  ans[l]=N[X.size()][Y.size()];
  l++;

  }
  cout<<ans[0]<<endl;
 cout<<ans[1]<<endl;
 cout<<ans[2]<<endl;

 return 0;
}

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

int main(){
    int N; cin >> N;
    for (int _ = 0; _ < N*2;_ += 2){
        string A,B;
        cin >> A >> B;
        
        vector<vector<int>> dp(A.size() +1 ,vector<int> (B.size() + 1,0));
        for (int i = 0;i < A.size();i ++){
            for(int j = 0; j < B.size(); j++){
                if(A[i] == B[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[A.size()][B.size()] << endl;
    }
}

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;
}