Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

#define SIZE 1010
class TLcsSolver
{
    int FTable[SIZE][SIZE];
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
    void Print()const
    {
        for(std::size_t i = 0; i < Fn1; ++i){
            for(std::size_t j = 0; j < Fn2; ++j){
                printf("%d\t", FTable[i][j]);
            }
            printf("\n");
        }
    } 
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[Fn1-1][Fn2-1];
    }
};

int main()
{
    int n;
    std::scanf("%d\n", &n);
    char a[SIZE];
    char b[SIZE];
    for(int i = 0; i < n; ++i){
        std::fgets(a, SIZE, stdin);
        std::fgets(b, SIZE, stdin);
        std::printf("%d\n", TLcsSolver(a, b).Answer());
    }
    return 0;
}

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

using namespace std;

int lcs (string x, string y) {

    int m, n, c[1001][1001], i, j, maxl = 0;

    m = x.size();
    n = y.size();

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

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

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

                maxl = max (maxl, c[i][j]);

            }
    }

    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 <iostream>
#include <cstdio>
#include <string>
#define MAX 1001

using namespace std;


string a,b;
int lcs_memo[1001][1001];

int lcs(int i,int j){
  if(i==0 || j==0) return 0;
  if(lcs_memo[i][j]!=-1) return lcs_memo[i][j];
  if(a[i]==b[j]){
    lcs_memo[i][j] = lcs(i-1,j-1)+1;
  }else{
    lcs_memo[i][j] = max(lcs(i,j-1),lcs(i-1,j));
  }
  return lcs_memo[i][j];
}

void clear(){
  for(int i=0;i<1001;i++){
  for(int j=0;j<1001;j++){
    lcs_memo[i][j] = -1;
  }}
}

int main(){
  int n; cin>>n;
  
  for(int i=0;i<n;i++){
    clear();
    cin>>a>>b;
    a = ' ' + a;
    b = ' ' + b;
    cout<<lcs(a.size()-1,b.size()-1)<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define SENTINEL (-1)
#define M (1001)

// ia, ib以降のindexに対するLCSを返す
// (ia, ib)に対するLCSがわかっているとき, (ia-1, ib), (ia, ib-1)
// に対するLCSは, a[ia-1] == a[ib],...であれば+1される....
// と考えたくなるが, iaとibでカウントされている場合, ダブルカウントになってしまうから,
// 必ずしもそうとは言えない.
// X: abcbdab
// Y: bdcaba
// 1-originで考えると,
// LCS(7,6,X,Y) = 0
// LCS(6,6,X,Y) = 1
// LCS(5,5,X,Y) = 1
// LCS(4,5,X,Y) = 2
// ... ????
// 部分問題に分解したい
// X: abc
// Y: ac
// -> 2
// X[3],Y[2] -> 1
// X[2],Y[2] -> 
// lcs(i,j)
// : if a[i] == b[j] => lcs(i,j) = lcs(i-1,j-1) + 1
// : else lcs(i,j) = max ( lcs(i-1,j), lcs(i, j-1) )
// lcs(0,0) := 0
// lcs(

int lcs(int i, int j, string &a, string &b, int memo[][M]) {
	if (i==0 || j==0) {
		memo[i][j] = 0;
		return 0;
	}
	if (memo[i][j] != SENTINEL) {
		return memo[i][j];
	}
	// i, j as 1-origin index
	if (a[i-1] == b[j-1]) {
		memo[i][j] = lcs(i-1, j-1, a, b, memo) + 1;
	} else {
		memo[i][j] = max(lcs(i-1,j,a,b,memo), lcs(i,j-1,a,b,memo));
	}
	return memo[i][j];
}

// この多次元配列の書き方は汎用性がなくて嫌
void fill(int a[][M], int size1, int size2, int v) {
	for (int i=0;i<size1;++i) {
		for (int j=0;j<size2;++j) {
			a[i][j] = v;
		}
	}
}

int main () {
	int q;
	cin >> q;
	int LCS[1001][M];
	fill(LCS, 1001, 1001, SENTINEL);// -1 is sentinel
	vector<int> res;
	for (int i=0; i<q; ++i) {
		string a, b;
		cin >> a >> b;
		res.push_back(lcs(a.size(),b.size(),a,b,LCS));
		fill(LCS, M, M, SENTINEL);
	}
	for (auto i : res) {
		cout << i << endl;
	}
	return 0;
}

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

int lcs(string X,string Z)
{
  int c[N+1][N+1];
  int m=X.size();
  int n=Z.size();
  int maxl=0;
  X=' '+X;
  Y=' '+Z;
  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 i=1;j<=n;j++)
	{
	  if(X[i]==Z[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()
{
  stirng 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 <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)
#define all(v) (v).begin(),(v).end()

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

struct edge{
    int from;
    int to;
    int weight;
    edge(int f,int t,int w) : from(f), to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

void solve(){
    string s,t;
    cin >> s >> t;
    int n = s.size();
    int m = t.size();
    vector<vector<int>> dp(n+1,vector<int>(m+1,0));
    rep(i,n) rep(j,m){
        if(s[i]==t[j]){
            dp[i+1][j+1] = max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
        }
        else{
            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
        }
    }
    cout << dp[n][m] << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    rep(_,t) solve();
}