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

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

#define N_MAX 1000

char X[N_MAX];
char Y[N_MAX];
int LCS[N_MAX+1][N_MAX+1];


int longest(int x, int y)
{
    for (int i = 0; i < x+1; i++) {
        for (int j = 0; j < y+1; j++) {
            if (i == 0 || j == 0) {
                LCS[i][j] = 0;
//                printf("0 LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
            else if (X[i-1] == Y[j-1]) {
                LCS[i][j] = LCS[i-1][j-1] + 1;
//                printf("1 LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
            else {
                LCS[i][j] = (LCS[i-1][j] > LCS[i][j-1]) ? LCS[i-1][j] : LCS[i][j-1]; 
//                printf("LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
        }
    }

    return LCS[x][y]; 
}

int main() {
    int q;
    int x;
    int y;

    cin >> q;

    for (int i = 0; i < q; i++) {
        scanf("%s", X);
        x = strlen(X);
        scanf("%s", Y);
        y = strlen(Y);
        cout << longest(x, y) << endl;
    }
        
    return 0;
}

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

int lcs(string str1, string str2)
{
	int len[1001][1001] = { 0 };

	int len1 = str1.length();
	int len2 = str2.length();
	str1 = ' ' + str1;
	str2 = ' ' + str2;

	for (int i = 0; i <= len1; i++)
		for (int j = 0; j <= len2; j++) len[i][j] = 0;

	for (int i = 1; i <= len1; i++)
	{
		for (int j = 1; j <= len2; j++)
		{
			if (str1[i] == str2[j]) len[i][j] = len[i - 1][j - 1] + 1;
			else len[i][j] = max(len[i - 1][j], len[i][j - 1]);
		}
	}
	return len[len1][len2];
}

int main()
{
	int n;
	string str1, str2;

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> str1 >> str2;
		cout << lcs(str1, str2) << endl;
	}

	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[1001][1001] = { 0 };
int main()
{
	int sy,sx, q, k;
	char x[2001], y[2001];
	k = 0;
	scanf("%d", &q);
	for (int z = 0; z < q; z++)
	{
		if (z != 0)
		{
			for (int i = 1; i <= sy; i++)
			{
				for (int j = 1; j <= sx; j++)
					dp[i][j] = 0;
			}
		}
		scanf("%s %s", x, y);
		sx = strlen(x);
		sy = strlen(y);
		for (int i = 1; i <= sy; i++)
		{
			for (int j = 1; j <= sx; j++)
			{
				if (y[i - 1] == x[j - 1])
					dp[i][j] += dp[i - 1][j - 1] + 1;
				else
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		printf("%d\n", dp[sy][sx]);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define For(I,A,B) for(int I=(A); I < (B); ++I)
int main()
{
int t;
cin>>t;
while(t--){
int a[1000]={0},b[1000]={0},c[200][200];
int x,y,z,k=0,l,i,j;

 For(i,0,200)
 For(j,0,200)
 c[i][j]=0;


 cin>>x>>y;
 For(i,0,x+1)
 For(j,0,y+1)
 c[i][j]=0;
 For(i,1,x+1) cin>>a[i];
 For(i,1,y+1) cin>>b[i];

 For(i,1,x+1)
 For(j,1,y+1)
 {
     if(a[i]==b[j]) c[i][j]=c[i-1][j-1]+1;
     else  c[i][j]=max(c[i-1][j],c[i][j-1]);
 }
    cout<<c[x][y]<<endl;

}
}
    return 0;
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[100000][100000];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1000;j++){
      for(int k=0;k<1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<x.size();j++){
      for(int k=0;k<y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}