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;

#define LOOP(val,times) for(val = 0;val < (times);++val)


int lcs(string X, string Y){
    int dp[1010][1010];
    int i,j;
    LOOP(i,1010){
        dp[0][i] = 0;
        dp[i][0] = 0;
    }
    LOOP(i,Y.size()){
        LOOP(j,X.size()){
            if(X[j] == Y[i]){
                dp[i+1][j+1] = dp[i][j] + 1;
            }
            else{
                dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
            }
        }
    }
    return dp[Y.size()][X.size()];    
}

int main(){
    int i,q;
    cin >> q;
    LOOP(i,q){
        string X,Y;
        cin >> X >> Y;
        cout << lcs(X,Y) << endl;
    }
    int k;cin >> k;
}

C++: #include <iostream>
#include <cassert>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
  int q;
  cin >> q;
  string s, t;
  int ls, lt;
  for (int p=0; p<q; ++p){
    cin >> s >> t;
    ls = s.length();
    lt = t.length();
    int dp[ls+1][lt+1];
    for(int i=0; i<=ls; ++i){
      for(int j=0; j<=lt; ++j){
        if(i == 0 || j == 0){
          dp[i][j] = 0;
        }
        else {
          dp[i][j] = (s[i-1] == t[j-1]) ? max(max(dp[i-1][j-1] + 1, dp[i-1][j]), dp[i][j-1]) : max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[ls][lt] << endl;
  }
}

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

#define MAX_N 1000
#define MAX_M 1000

int n;
string s, t;

int dp[MAX_N + 1][MAX_M + 1];

void solve(){
	for(int i = 0; i < s.size(); i++){
		for(int j = 0; j < t.size(); j++){
			if(s[i] == t[j]){
				dp[i + 1][j + 1];
					dp[i + 1][j + 1] = dp[i][j] + 1;
				} else {
					dp[i + 1][j + 1] = max(dp[i][j], dp[i + 1][j]);
				}
			}
		}
		cout << dp[s.size()][t.size()] << endl;
}

int main(){
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> s >> t;
		solve();
	}
}

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

string X, Y;
int v[1001][1001];

int  dp(string X, string Y) {
	int n = X.size();
	int m = Y.size();
	memset(v, 0, sizeof(v));
	for (int i = 0; i <= n; i++) {
		v[i][0] = 0;											///０のテーブル
		for (int j = 0; j <= m; j++) {
			v[0][j] = 0;
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (X[i-1] == Y[j-1]) {
				v[i][j] = v[i - 1][j - 1] + 1;
			}
			else {
				v[i][j] = max(v[i - 1][j], v[i][j - 1]);
			}
		}
	}
	return v[n][m];
}

int main(void)
{
	int x;
	cin >> x;
	
	for (int i = 0; i < x; i++) {
		cin >> X >> Y;
		int ans = dp(X, Y);
		cout << ans << endl;
	}
	
	system("pause");
}

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

using namespace std;                                         
                                                             
//L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS                        
int L[1000][1000]={};                                        
int main(){
  int testnum;
  cin>>testnum;                                              
  for(int t=0;t<testnum;++t){                                
    string X,Y;                                              
    cin>>X>>Y;
    
    for(int i=0;i<X.length();++i){                           
      for(int j=0;j<Y.length();++j){                         
        if(X[i]==Y[j])
          L[i][j] = 1 + L[i-1][j-1];                         
        else
          L[i][j] = max(L[i][j-1],L[i-1][j]);
      }
    }

    cout << L[X.length()-1][Y.length()-1] << endl;
  }

  return 0;
}

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

using namespace 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 res = 0;

    x = ' ' + x;
    y = ' ' + y;

    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] == y[j]) {
                c[i][j] = c[i - 1][j - 1] + 1;
            } else {
                c[i][j] = max(c[i][j - 1], c[i - 1][j]);
            }
            res = max(res, c[i][j]);
        }
    }

    return res;
}

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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <numeric>
#include <climits>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
int n, m;
string s, t;
const int n_max = 1000;
const int m_max = 1000;

int main() {
	cin >> n;
	for(int k=0; k<n; ++k){
        cin >> s >> t;
        int dp[n_max+1][m_max+1] = {0};
	for (int i = 0; i < s.size(); ++i) {
		for (int j = 0; j < t.size(); ++j) {
			if (s[i] == t[j]) {
				dp[i + 1][j + 1] = dp[i][j] + 1;
			}
			else {
				dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
			}
		}
	}
        cout << dp[s.size()][t.size()] << endl;
    }
	

	return 0;
}

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

#define MAX 1000

int dp[MAX+1][MAX+1];

int lcs(char x[], char y[]){
  for(int i = 0; i<=strlen(x); i++){
    dp[i][0] == 0;
    dp[0][i] == 0;
  }

  for(int i = 1; i<=strlen(x); i++){
    for(int j = 1; j<=strlen(y); j++){
      dp[i][j] = (x[i] == y[j]) ? dp[i-1][j-1] + 1 :
                                  max(dp[i-1][j],dp[i][j-1]);
    }
  }

  return dp[strlen(x)][strlen(y)];
}

int main(){

  int q;
  scanf("%d", &q);

  char X[MAX + 1];
  char Y[MAX + 1];
  for(int i=0; i<q; i++){
    scanf("%s\n%s\n", X, Y);
    printf("%d\n", lcs(X, Y));
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <string>
#include <utility>
using namespace std;

int n;
string a,b;


int main(){
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a >> b;
		int t[1010][1010] = {};
		for (int i = 0; i < a.length(); i++){
			for (int j = 0; j < b.length(); j++){
				if (i == 0 || j == 0)t[i][j] = 0;
				else if (a[i] == b[j]){
					t[i + 1][j + 1] = t[i][j] + 1;
				}
				else t[i + 1][j + 1] = max(t[i + 1][j], t[i][j + 1]);
			}
		}
		cout << t[a.length()][b.length()] << endl;
	}
	return 0;
}