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<stdio.h>
#include<string.h>
   
int LCS(char *X, char *Y){
  int i,j,m,n,c[1001][1001];
  m = strlen(X);
  n = strlen(Y);
     
  for(i=0; i<m; i++){
    c[i][0] = 0;
  }
  for(j=0; j<n; j++){
    c[0][j] = 0;
  }
   
  for(i=1;i<m+1;i++){
    for(j=1;j<n+1;j++){
      if (X[i-1]==Y[j-1]) c[i][j]=c[i-1][j-1]+1;
      else if (c[i-1][j]>=c[i][j-1]) c[i][j]=c[i-1][j];
      else c[i][j]=c[i][j-1];
    }
  }
  return c[i-1][j-1];
}
   
int main(){
  int i, q;
  char X[1001], Y[1001];
     
  scanf("%d", &q);
  for(i=0; i<q; i++){
    scanf("%s", X);
    scanf("%s", Y);
    printf("%d\n", LCS(X, Y));
   
  }
  return 0;
}

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<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()];

  cout<<N[X.size()][Y.size()]<<endl;
  // l++;

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

 return 0;
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int dp[1050][1050];
int main(){
	int n;
	cin>>n;
	while(n--){
		memset(dp, 0, sizeof(dp));
		string str1,str2;
		cin>>str1>>str2;
		//int l =min(str1.length(),str2.length());
		for(int i =0;i <str1.length();i++){
			for(int j =0;j <str2.length();j++){
				if(str1[i]==str2[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[str1.length()][str2.length()]<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
int dp[1010][1010];
int main(){
  int q;
  cin >> q;
  for(int i=0;i<q;i++){
    for(int j=0;j<1010;j++){
      fill(dp[j],dp[j]+1010,0);
    }
    string a,b;
    cin >> a >> b;
    for(int j=0;j<a.size();j++){
      for(int k=0;k<b.size();k++){
        if(a[j] == b[k]){
          dp[j+1][k+1] = dp[j][k] + 1;
        }else{
          dp[j+1][k+1] = max(dp[j+1][k] ,dp[j][k+1]);
        }
      }
    }
    cout << dp[a.size()][b.size()] << endl;
  }
  return 0;
}

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

using namespace std;

int main () {

    int N, m, n, i, j, k, c[1001][1001];
    string x, y;

    cin >> N;

    for (k = 0; k < n; k++) {
        cin >> x >> y;
        m = x.size ();
        n = y.size ();

        for (i = 1; i <= m; i++) {
            for (j = 1; j <= n; j++) {

                if (x[i - 1] == y[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[m][n] << endl;

    }

}

C++: #include<stdio.h>
#include<string>
#include<algorithm>
using namespase 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<bits/stdc++.h>
using namespace std;
#define N 1050
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=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(){
  int q;cin>>q;
  while(q--){
    string a,b;cin>>a>>b;
    cout<<lcs(a,b)<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<stdlib.h>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>

#define Max 1005
using namespace std;

int N;

int Long(string A, string B)
{
	int a = A.length();
	int b = B.length();
	int L[Max][Max];
	int maxi = 0;
	for (int i = 0; i < a; i++)
	{
		L[i][0] = 0;
	}

	for (int i = 0; i < b; i++)
	{
		L[0][i] = 0;
	}

	for (int i = 1; i < a; i++)
	{
		for (int j = 1; j < b; j++)
		{
			if (A[i] == B[j])
				L[i][j] = L[i - 1][j - 1] + 1;
			else
				L[i][j] = max(L[i - 1][j], L[i][j - 1]);
			maxi = max(maxi, L[i][j]);
		}
	}
	return maxi;
}

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string A;
		string B;
		cin >> A;
		A = ' ' + A;
		cin >> B;
		B = ' ' + B;
		cout << Long(A, B) << endl;
	}
	return 0;
}