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<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX_N 1000

int solve(std::string s,std::string t){
        int dp[MAX_N][MAX_N];
        memset(dp,0,sizeof(dp));
        int n,m;
        n=s.size(),m=t.size();
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(s[i]==t[j])
                                dp[i+1][j+1]=dp[i][j]+1;
                        else
                                dp[i+1][j+1]=std::max(dp[i][j+1],dp[i+1][j\
]);
                }
        }
        return dp[n][m];
}

int main(){
        int q;
        std::cin>>q;
        for(int i=0;i<q;i++){
                std::string s,t;
                std::cin >> s>>t;
                std::cout << solve(s,t) << std::endl;
        }
        return 0;
}

C++: #if 0

#endif

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

int main(){
	int q;
	string X,Y;
	string x,y;
	int L[1000][1000];
	int MAX;
	for(int i=0;i<1000;++i) //???????????\????????????L????????????
		for(int j=0;j<1000;++j)
			L[i][j]=0;
	
	cin >> q;
	for(int i=0;i<q;++i){
		cin >> X >> Y;
		for(int j=1;j<X.length()+1;++j)
			for(int k=1;k<Y.length()+1;++k){
				if(X[j-1]==Y[k-1])
					L[j][k]=1+L[j-1][k-1];
				else
					L[j][k]=max(L[j-1][k],L[j][k-1]);
				MAX = max(MAX,L[j][k]);
			}
	cout << MAX << endl;
	}
}

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

int main()
{
  int q;scanf("%d",&q);
  for(int Q=0;Q<q;Q++)
  {
  string X,Y;cin>>X>>Y;
  vector<vector<int>> dp(X.size()+1,vector<int>(Y.size()+1,0));
  for(int i=0;i<(int)X.size();i++)for(int j=0;j<(int)Y.size();j++)
  {
    if(X[i]==Y[j])dp[i+1][j+1]=dp[i][j]+1;//max({dp[i][j]+1,dp[i][j+1],dp[i+1][j]});
    else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
  }
  printf("%d\n",dp[X.size()][Y.size()]);
  }
}

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

int LCS(string p, string q) {
	vector<vector<int> > f(p.size()+1, vector<int>(q.size()+1,0));
	int t;
	for (int i = 1; i <= p.size(); i++) {
		for (int j = 1; j <= q.size(); j++) {
			if (p[i-1] == q[j-1])
				f[i][j] = f[i - 1][j - 1] + 1;
			else if (f[i][j - 1] > f[i - 1][j])
				f[i][j] = f[i][j - 1];
			else
				f[i][j] = f[i - 1][j];
		}
	}
	return f[p.size()][q.size()];
}

int main() {
	int n;
	string p[300];
	cin >> n;
	for (int i = 0; i < n; i ++) {
		cin >> p[2*i] >> p[2*i + 1];
	}
	for (int i = 0; i < n; i++) {
		cout << LCS(p[2*i], p[2 * i + 1])<<endl;
	}
	return 0;
}

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

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

/// 最小共通部分列: longest common subsequence
/// O(|S||T|)
int lcs(const string& s, const string& t) {
    int slen = s.size(), tlen = t.size();
    vector<vector<int>> dp(slen + 5, vector<int>(tlen + 5, 0));
    rep(i, 0, slen) rep(j, 0, tlen) {
        if (s[i] == t[j]) {
            chmax(dp[i + 1][j + 1], dp[i][j] + 1);
        }
        chmax(dp[i + 1][j + 1], dp[i + 1][j]);
        chmax(dp[i + 1][j + 1], dp[i][j + 1]);
    }

    return dp[slen][tlen];
}
//---------------------------------------------------------------------------------------------------

signed main() {
    int n;
    cin >> n;
    rep(i, 0, n) {
        string s, t;
        cin >> s >> t;
        cout << lcs(s, t) << "\n";
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }

            }
        }
        printf("%d\n", dp[xlen][ylen]);
        memset(dp, 0, sizeof(int)*1001*1001);
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#define MAX_N 1001
using namespace std;
//lcs
int n,m;
string s,t;
int dp[MAX_N+1][MAX_N+1];
void solve(){
  for(int i=0;i<n;i++)
    for(int j=0;j<m;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]);
    }
  cout<<dp[n][m]<<endl;
}
 
 
int main(){
  int N;
  cin>>N;
  while(N--){
    cin>>s>>t;
    n=s.size();
    m=t.size();
    solve();
  }
  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class t> inline string to_str(t x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int n, dp[1024][1024];
	string a, b;
	cin >> n;
	
	while(n)
	{
		n--;
		memset(dp, 0, sizeof(dp));

		cin >> a;
		cin >> b;

		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] = max(dp[i][j] + 1, max(dp[i+1][j], dp[i][j+1]));
				}
				else
				{
					dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
				}
			}
		}
		/*
		for(int i = 0; i <= a.size()+1; i++)
		{
			for(int j = 0; j <= b.size()+1; j++)
			{
				printf("%3d ",dp[i][j]);
			}
			puts("");
		}
		*/

		cout << dp[a.size()][b.size()] << endl;
	}
}