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 <vector>
#include <iostream>
              
void solve() {
  std::string X, Y;
  std::cin >> X >> Y;
  const int LX = X.size();
  const int LY = Y.size();
  std::vector<int> dp0(LY+1, 0), dp1(LY+1, 0);
  std::vector<int> *dp = &dp0;
  std::vector<int> *dp_ = &dp1;
  for (int i = 1; i < LX+1; i++) {
    std::swap(dp, dp_);
    for (int j = 1; j < LY+1; j++) {
      (*dp)[j] = std::max((*dp)[j-1], (*dp_)[j]);
      if (X[i-1] == Y[j-1]) {
        (*dp)[j] = std::max((*dp)[j], (*dp_)[j-1] + 1);
      }       
    }         
  }           
  std::cout << dp->back() << std::endl;
}             
              
int main() {  
  int N;      
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    solve();  
  }           
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#define MAX 1000


int lcs(std::string X, std::string Y) {
    int c[MAX+1][MAX+1];
    int maxl = 0;
    int m = X.size();
    int n = Y.size();
    for (int i = 1; i <= m; i++) {
        c[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
        c[0][i] = 0;
    }
    X = ' ' + X;
    Y = ' ' + Y;
    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] = std::max(c[i-1][j], c[i][j-1]);
            }
            maxl = std::max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main() {
    std::string s1, s2;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        std::cin >> s1 >> s2;
        int a = lcs(s1, s2);
        printf("%d\n", lcs(s1, s2));
    }
    
    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; // X[0] に空白を挿入
	Y = ' ' + Y; // Y[0] に空白を挿入
	for ( int i = 0; 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<bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define pd(a,n) for(int i=0;i<n;i++)cout<<a[i]<<((i==n-1)?"\n":" ");cout<<endl;
#define pdd(a,n,m)for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define mp make_pair
#define pb push_back
#define ll long long
//#define __int64 long long
#define P pair<int,int>
#define PP pair<P,int>

int dp[1011][1011];

int query(string a,string b){
  int n=a.size();
  int m=b.size();

  rep(i,1011)rep(j,1011)dp[i][j]=0;

  rep(i,n){
    rep(j,m){
      if(a[i]==b[j]){
        dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
      }
      dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
      dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
    }
  }
  int ret=0;
  rep(i,n+1)rep(j,m+1)ret=max(ret,dp[i][j]);

  return ret;
}
int main(){
  int q;
  cin>>q;
  rep(i,q){
    string a,b;
    cin>>a>>b;
    cout<<query(a,b)<<endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
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(void)
{
    string X, Y;
    int n; cin >> n;

    for (int i = 0; i < n; ++i ) {
        cin >> X >> Y;
        cout << LCS(X, Y) << endl;
    }
    return 0;
}

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

string s, t;
int dp[100][100];

void solve() {
    cin >> s >> t;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < t.size(); j++) {
            dp[i][j] = (i < 1 || j < 1 ? 0 : dp[i-1][j-1]) + (s[i] == t[j] ? 1 : 0);
            int a = (i < 1 ? 0 : dp[i-1][j]);
            int b = (j < 1 ? 0 : dp[i][j-1]);
            dp[i][j] = max(dp[i][j], max(a, b));
        }
    }

    cout << dp[s.size()-1][t.size()-1] << endl;
}

int main() {
    int n;
    cin >> n;
    while (n--) solve();

    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i,n)   for (int i = 0; i < (n); ++i)
#define SORT(a)    sort((a).begin(), (a).end());
#define UNIQ(a)    SORT(a);(a).erase(unique((a).begin(), (a).end()), (a).end());
#define FIND(a,x)  find((a).begin(), (a).end(), (x)) != (a).end()
#define REVERSE(a) reverse((a).begin(), (a).end());

using namespace std;
using ll = long long;
using P = pair<int,int>;

const int MOD = 1000000007; // 10^9 + 7

int lcs(string x, string y) {
  int m = x.size(), n = y.size();
  int dp[m + 1][n + 1]; memset(dp, 0, sizeof(dp));

  REP(i, m)REP(j, n)
    if (x[i] == y[j])
      dp[i + 1][j + 1] = dp[i][j] + 1;
    else
      dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);

  return dp[m][n];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));

  int t; cin >> t;
  REP(i, t) {
    string x, y; cin >> x >> y;

    cout << lcs(x, y) << endl;
  }
}

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

int main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    vector<vector<int>> dp(x.size()+1,vector<int>(y.size()+1,0));
    
    for(int xi=0;xi<x.size();++xi){
      for(int yi=0;yi<y.size();++yi){
        if(x[xi]==y[yi]){
          dp[xi+1][yi+1]=dp[xi][yi]+1;
        }else{
          if (dp[xi+1][yi]>dp[xi][yi+1]){
            dp[xi+1][yi+1]=dp[xi+1][yi];
          }else{
            dp[xi+1][yi+1]=dp[xi][yi+1];
          }
        }
      }
    }
    int ans=dp[x.size()][y.size()];
    cout << ans << endl;
  }
}