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>

int score[1010][1010];

int max2 (int a, int b){
  if (a >= b) return a;
  else return b;
}

int len_of_str(const char s[]){
  int num = 0;

  while(s[num]) num++;

  return num;
}


int main() {
  char a[1010];
  char b[1010];
  int datanum;
  scanf("%d",&datanum);
  for(int c = 0; c < datanum; c++){

  scanf("%999s",a);
  scanf("%999s",b);
  int num1 = len_of_str(a);
  int num2 = len_of_str(b);


  for(int i = num1; i > 0; i--) a[i] = a[i - 1];
  a[0] = ' ';
  for(int j = num2; j > 0; j--) b[j] = b[j - 1];
  b[0] = ' ';

  for(int i = 0; i <= num1; i++) score[i][0] = 0;
  for(int j = 0; j <= num2; j++) score[0][j] = 0;

  int maximum = 0;

  for(int i = 1; i<= num1; i++){
    for (int j = 1; j <= num2; j++){
      if (a[i] == b[j])score[i][j] = score[i-1][j-1] + 1;
      else max2(score[i][j-1],score[i-1][j]);
      maximum = max2(maximum,score[i][j]);
    }
  }

  printf("%d\n",maximum);
}
  return 0;
}

C++: #include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll q;
  cin >> q;
  for (ll testcase = 0; testcase < q; ++testcase) {
    string a, b;
    cin >> a >> b;
    vector<vector<ll>> dp(a.size() + 1, vector<ll>(b.size() + 1, 0));
    for (ll i = 0; i < a.size(); ++i) {
      for (ll j = 0; j < b.size(); ++j) {
        if (a.at(i) == b.at(j)) {
          dp.at(i + 1).at(j + 1) = max(dp.at(i + 1).at(j + 1), dp.at(i).at(j) + 1);
        }
        dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
        dp.at(i).at(j + 1) = max(dp.at(i).at(j + 1), dp.at(i).at(j));
        dp.at(a.size()).at(j + 1) = max(dp.at(a.size()).at(j), dp.at(a.size()).at(j + 1));
      }
      dp.at(i + 1).at(b.size()) = max(dp.at(i + 1).at(b.size()), dp.at(i).at(b.size()));
    }
    cout << dp.at(a.size()).at(b.size()) << "\n";
    // for (ll i = 0; i <= a.size(); ++i) {
    //   for (ll j = 0; j <= b.size(); ++j) {
    //     cout << dp.at(i).at(j) << " ";
    //   }
    //   cout << "\n";
    // }
  }
}

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

int lcs(string X,string Y)
{
    int x=X.size(),y=Y.size();
    int dp[y+1][x+1];

    for(int i=0;i<x+1;i++)dp[0][i]=0;
    for(int i=1;i<y+1;i++)dp[i][0]=0;
    int maxl=0;
    
    for (int j = 1; j <= y; j++){
        for (int k = 1; k <= x ; k++){
            if (Y[j-1]==X[k-1]) dp[j][k]=dp[j-1][k-1]+1;
            else dp[j][k]=max(dp[j][k-1],dp[j-1][k]);
            maxl=max(maxl,dp[j][k]);
        }
    }
    return maxl;
}

int main()
{
    int q;
    cin>>q;
    string X,Y;
    for (int i = 0; i < q; i++) {
        cin>>X>>Y;
        cout<<lcs(X,Y)<<endl;
    }
}
/*
3
abcbdab
bdcaba
abc
abc
abc
bc
*/

C++: #include "stdio.h"
#include<iostream>
#include<string>
#include<algorithm>

#define MAX 1000

using namespace std;

int lcs(string input1, string input2){
  int m = input1.length();
  int n = input2.length(); 

  input1 = " " + input1;
  input2 = " " + input2; 

  //Singleton?
  int c[MAX+1][MAX+1];
    
  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(input1[i] == input2[j]){
        c[i][j] = c[i-1][j-1] + 1;
      }else{
        c[i][j] = max(c[i-1][j], c[i][j-1]);
      }
    
    }     
  }
  return c[m][n];
}

int main(void){
  
  int N;
  scanf("%d", &N);

  string  input1, input2;

  for(int i=0;i<N;i++){
    cin >> input1;
    cin >> input2;
    printf("%d\n", lcs(input1, input2));
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
typedef long long ll;
int lcs(string x,string y){
    int dp[1001][1001];
    int maxl=0;
    int X=x.size(),Y=y.size();
    x=" "+x;y=" "+y;
    rp(j,X+1)dp[j][0]=0;
    rp(j,Y+1)dp[0][j]=0;
    rep(j,1,X+1){
        rep(k,1,Y+1){
            if(x[j]==y[k])dp[j][k]=dp[j-1][k-1]+1;
            else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
        }
    }
    return dp[X][Y];
}
int main(){
    int q;cin>>q;
    rp(i,q){
        string s1,s2;
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
}

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

using namespace std;


int lcs(string x, string y) {
    int c[1001][1001];
    int n = x.length();
    int m = y.length();
    for(int i = 0; i < n; i++) {
        c[i][0] = 0;
    }
    for(int j = 0; j < m; j++) {
        c[0][j] = 0;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; 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[n][m];
}

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

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

std::string x,y;
int lcs[1000][1000];

int max(int k,int l){
  if(k>l){
    return k;
  }else{
    return l;
  }
}

void LCS(int i,int j){
  if(x[i] == y[j]){
    lcs[i][j] = lcs[i-1][j-1]+1;
  }else{
    lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1]);
  }
}

void pre(){
  for(int i=0;i<(int)y.size();i++){
    if(x[0] == y[i]){
      for(int j=i;j<(int)y.size();j++){
	lcs[0][j] = 1;
      }
      break;
    }else{
      lcs[0][i] = 0;
    }
  }

  for(int i=0;i<(int)x.size();i++){
    if(x[i] == y[0]){
      for(int j=i;j<(int)x.size();j++){
	lcs[j][0] = 1;
      }
      break;
    }else{
      lcs[i][0] = 0;
    }
  }

}

int main(){
  int q;
  std::cin >> q;
  for(int k=0;k<q;k++){
    std::cin >> x;
    std::cin >> y;
    int size_x=x.size(),size_y=y.size();
    pre();
    for(int i=1;i<size_x;i++){
      for(int j=1;j<size_y;j++){
	LCS(i,j);
      }
    }

    /*for(int i=0;i<size_x;i++){
      for(int j=0;j<size_y;j++){
	std::cout << lcs[i][j] << ' ';
      }
      std::cout << '\n';
    }
    */
    
    std::cout << lcs[size_x-1][size_y-1] << '\n'; 

  }
  return 0;
}