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;

int lcs(string s1, string s2) {
    int n = s1.size(), m = s2.size();

    int c[n + 1][m + 1];
    for (int i = 0; i <= n; i++)
    {
        c[i][0] = 0;
    }
    for (int i = 0; i <= m; i++) {
        c[0][i] = 0;
    }

    // indexを合わせるため、各文字列に空白を挿入
    s1 = ' ' + s1;
    s2 = ' ' + s2;

    int maxl=0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++) {
            if (s1.at(i) == s2.at(j))
                c[i][j] = 1 + c[i - 1][j - 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;

    for (int i = 0; i < q; i++)
    {
        string s1, s2;
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: def lcs(str_a,str_b):    
    if len(str_a) == 0 or len(str_b) == 0:
        return 0
    dp = [[0 for kb in range(len(str_b) + 1)] for ka in range(len(str_a) + 1)]
    for i in range(1, len(str_a) + 1):
        for j in range(1, len(str_b) + 1):
            if str_a[i-1] == str_b[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max([dp[i-1][j], dp[i][j-1]])
  #print ("length of LCS is :",dp[len(str_a)][len(str_b)])
    return dp[len(str_a)][len(str_b)]

n=int(input())
for i in range(n):
    s1=input()
    s2=input()
    print(lcs(s1,s2))

C++: #include <bits/stdc++.h>
#define REP0(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPB0(i,n) for(int i=n-1;i>=0;i--)
#define REPB1(i,n) for(int i=n;i>0;i--)
#define llong long long
#define INF 2000000000
#define NIL -1
#define N 1000
using namespace std;
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;
  REP1(i,m) C[i][0]=0;
  REP1(i,n) C[0][i]=0;
  REP1(i,m){
    REP1(j,n){
      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(){
  char a[1000],b[1000];
  string s1,s2;
  int n;
  scanf("%d",&n);
  REP0(i,n){

    scanf("%s %s",a,b);
    s1=a;s2=b;
    printf("%d\n",LCS(s1,s2));
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int MAX=1000;

int LCS(string X, string Y) {
    int i, j, maxl=0;
    int C[MAX+1][MAX+1];

    int X_len = X.size();
    int Y_len = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    for (i=0; i<=X_len; i++) C[i][0]=0;
    for (j=1; j<=Y_len; j++) C[0][j]=0;

    for (i=1; i<=X_len; i++) {
        for (j=1; j<=Y_len; j++) {
            if (X[i]==Y[j]) {
                C[i][j]=C[i-1][j-1]+1;
                maxl = max(C[i][j], maxl);
            } else C[i][j]=max(C[i-1][j], C[i][j-1]);
        }
    }

    return maxl;
}

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

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
int main(int argc, const char * argv[]) {
    int q, i, m, n, sl1, sl2;
    cin >> q;
    vector<string> strlist;
    string str, str1, str2;
    for(i = 0;i < q;i++){
        cin >> str;
        strlist.push_back(str);
        cin >> str;
        strlist.push_back(str);
    }
    
    //start
    for(i = 0;i < q*2;i += 2){
        str1 = strlist[i];
        sl1 = str1.length();
        str2 = strlist[i+1];
        sl2 = str2.length();
        int dp[sl1+1][sl2+1];
        for(m = 0;m <= sl2;m++){
            dp[0][m] = 0;
        }
        for(m = 0;m <= sl1;m++){
            dp[m][0] = 0;
        }
        for(m = 1;m <= sl1;m++){
            for(n = 1;n <= sl2;n++){
                if(str1[m] == str2[n]){
                    dp[m][n] = dp[m-1][n-1] + 1;
                }else{
                    dp[m][n] = max(dp[m-1][n], dp[m][n-1]);
                }
            }
        }
        m = sl1;
        n = sl2;
        printf("%d\n", dp[m][n]);
        for(m = 1;m <= sl1;m++){
            for(n = 1;n <= sl2;n++){
                printf(" %d ", dp[m][n]);
            }
            printf("\n");
        }
        printf("\n");
        
    }
    return 0;
}

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



int lcs(string X,string Y){
	
	int c[720][720];
int m = X.size();
int n = Y.size();
int max1 = 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]);
		}
		max1 = max(max1,c[i][j]);
		}
	} 
	return max1;
}



int main(){
	int q;
	string s1,s2;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s1>>s2;
		cout<<lcs(s1,s2)<<endl;
	}
	return 0;
}

C++: //
//  main.cpp
//  ALDS1_10_C Longest Common Subsequence
//
//  Created by Kang on 2020/8/13.
//  Copyright © 2020 Kang. All rights reserved.
//

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char X[1001];
char Y[1001];

int LCS(int m, int n) {
    int c[1001][1001];
    for (int i = 0; i <= m; i++) {
        c[i][0] = 0;
    }
    for (int i = 0; i <= n; i++) {
        c[0][i] = 0;
    }
    
    for (int i = 1; i <= m; i++) {
        for (int 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][j - 1], c[i - 1][j]);
            }
            
        }
    }
    return c[m][n];
}

int main(int argc, const char * argv[]) {
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%s", X);
        scanf("%s", Y);
        printf("%d\n", LCS((int)strlen(X), (int)strlen(Y)));
    }
    return 0;
}