Go: package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	for q := 0; q < n; q++ {
		var s1, s2 string
		fmt.Scan(&s1, &s2)
		s1, s2 = " " + s1, " " + s2

		dp := make([][]int, len(s1))
		for i := range dp {
			dp[i] = make([]int, len(s2))
		}

		max := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					dp[i][j] = dp[i-1][j-1] + 1
				} else {
					dp[i][j] = Max(dp[i-1][j], dp[i][j-1])
				}
				max = Max(max, dp[i][j])
			}
		}

		fmt.Println(max)
	}
}

func Max(n ...int) int {
	max := n[0]
	for _, v := range n[1:] {
		if v > max {
			max = v
		}
	}
	return max
}

C++: #include<iostream>
using namespace std;
/*int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};*/
int main()
{
    int num;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        int lcs[1001][1001]={};
        string a,b;
        cin>>a>>b;
        //cout<<lcs(a,b)<<endl;
        for(int i=0;i<a.length()+1;i++)
        {
            for(int j=0;j<b.length()+1;j++)
            {
                if(i==0||j==0)
                {
                    lcs[i][j]=0;
                }
                else if(a[i]==b[j])
                {
                    lcs[i][j]=lcs[i-1][j-1]+1;
                }
                else
                {
                    lcs[i][j]=max(lcs[i][j-1],lcs[i-1][j]);
                }
            }
        }
        cout<<lcs[a.length()][b.length()]<<endl;
    }
}

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

int dp[1002][1002] = { 0 };

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		string x, y;
		int cnt = 0;
		cin >> x >> y;
		memset(dp, 0, sizeof(dp));

		for (int j = 1; j <= x.size(); ++j) {
			for (int j2 = 1; j2 <= y.size(); ++j2) {
				if (x[j - 1] == y[j2 - 1]) {
					dp[j][j2] = max(dp[j][j2 - 1], dp[j - 1][j2 - 1] + 1);
				}
				else {
					dp[j][j2] = max(dp[j][j2 - 1], dp[j - 1][j2]);
				}
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}

	return 0;
}

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

int main(){
        int q;
        scanf("%d",&q);
        while(q--){
                string str1,str2;
                cin >> str1 >> str2;
                vector<vector<int>> dp(str1.size()+1,vector<int>(str2.size()+1,0));
                for(int i = 0; i < str1.size(); i++){
                        for(int j = 0; j < str2.size(); 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.size()][str2.size()] << endl;
        }
        return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <vector>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string& s, std::string& t) {
  std::vector<std::vector<int> > dp(s.size() + 1, std::vector<int>(t.size() + 1, 0));

  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      else if (dp[i - 1][j] >= dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
      }
      else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

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

  return 0;
}

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

int main()
{
    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        string X, Y;
        cin >> X >> Y;
        int x, y;
        x = X.size();
        y = Y.size();
        // dp[i][j] := X[i], Y[j] まで見た時のLCS
        int dp[x + 5][y + 5];
        for (int i = 0; i < x + 5; i++) {
            for (int j = 0; j < y + 5; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (X[i] == Y[j]) {
                    dp[i + 1][j + 1] = max({ dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1] });
                } else {
                    dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
                }
            }
        }
        printf("%d\n", dp[x][y]);
    }
}

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

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int N;
  cin>>N;
  string S1,S2;
  for (int k=0; k<N; k++) {
    cin>>S1>>S2;
    int m=S1.length();
    int n=S2.length();
    S1=' '+S1;
    S2=' '+S2;
    int lcs[1001][1001];
    for (int i=1; i<=m; i++) {
      lcs[i][0]=0;
    }
    for (int j=1; j<=n; j++) {
      lcs[0][j]=0;
    }
    int maxl=0;
    for (int i=1; i<=m; i++) {
      for (int j=1; j<=n; j++) {
        if (S1[i]==S2[j]) {
          lcs[i][j]=lcs[i-1][j-1]+1;
        }
        else {
          lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
        }
        maxl=max(maxl,lcs[i][j]);
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include<string.h>
using namespace std;
int LCS(char *x,char *y);
char x[1001], y[1001];
int lcs[1001][1001];
int main()
{
	int q,i,j;

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> x;
		cin >> y;
		cout << LCS(x, y) << endl;
	}
}

int LCS(char *x, char *y)
{
	int i, j;
	int len_x, len_y;
	len_x = strlen(x);
	len_y = strlen(y);
	
	for (i = 0; i < len_x; i++) {
		if (x[0] == y[i]) {
			lcs[0][i] = 1;
		}
		else {
			lcs[0][i] = 0;
		}
	}
	for (i = 0; i < len_y; i++) {
		if (x[i] == y[0]) {
			lcs[i][0] = 1;
		}
		else {
			lcs[i][0] = 0;
		}
	}

	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			if (x[i] == y[j]) {
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
				//printf("lcs[%d][%d]=%d\n", i, j, lcs[i - 1][j - 1] + 1);
			}
			if (x[i] != y[j]) {
				lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
				//printf("lcs[%d][%d]=%d\n", i, j, max(lcs[i - 1][j], lcs[i][j - 1]));
			}
		}
	}
	//cout << endl;

	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			//printf("%d ", lcs[i][j]);
		}
		//cout << endl;
	}
	//cout << "len_x=" << len_x << endl << "len_y=" << len_y << endl;
			
	return lcs[len_x-1][len_y-1];
}

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

int dp[1010][1010];

int main() {
	int q;
	cin >> q;
	vector<string> x(q), y(q);
	for (int i = 0;i < q;i++) {
		cin >> x[i] >> y[i];
	}

	for (int i = 0;i <= 1000;i++) {
		dp[i][0] = 0;
		dp[0][i] = 0;
	}
	for (int i = 0;i < q;i++) {
		for (int j = 1;j <= x[i].size();j++) {
			for (int k = 1;k <= y[i].size();k++) {
				dp[j][k] = 0;
			}
		}
		for (int j = 1;j <= x[i].size();j++) {
			for (int k = 1;k <= y[i].size();k++) {
				dp[j][k] = max(dp[j - 1][k], dp[j][k - 1]);
				if (x[i][j-1] == y[i][k-1]) {
					dp[j][k] = max(dp[j][k], dp[j - 1][k - 1] + 1);
				}
			}
		}
		cout << dp[x[i].size()][y[i].size()] << endl;
	}

	return 0;
}