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<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }

  cout<<N[X.size()][Y.size()];
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }

}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

#ifdef DEBUG

#define printd printf

#else

#define printd(...)

#endif


int lcs(std::string &x, std::string &y) {
    static int dp[1002][1002];

    for (int i = 0; i < x.length() + 2; i++) {
        for (int j = 0; j < y.length() + 2; j++) {
            dp[i][j] = 0;
        }
    }

    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            if (x[i - 1] == y[j - 1]) {
                dp[i][j] = std::max(dp[i - 1][j - 1] + 1, dp[i - 1][j]);
                dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
            } else {
                dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    #ifdef DEBUG
    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            std::cout << dp[i][j] << ", ";
        }
        std::cout << std::endl;
    }
    #endif

    return dp[x.length()][y.length()];
}

int main() {
    int q;
    std::cin >> q;

    for (int qi = 0; qi < q; qi++) {
        std::string x, y;
        std::cin >> x >> y;
        std::cout << lcs(x, y) << std::endl;
    }

    return 0;
}

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

constexpr int nil = -1;

class ILcs
{
public:
    using vvi = std::vector<std::vector<int>>;
    virtual int lcs() = 0;
    virtual int lcs(int i1, int i2) = 0;
};

class LcsLoop : public ILcs
{
public:

    LcsLoop(std::string str1, std::string str2)
        : str1 {str1}
        , str2 {str2}
        , dp {vvi(str1.size(), std::vector<int>(str2.size()))}
    {
        CalcLcs();
    }

    int lcs() override
    {
        return lcs(str1.size() - 1, str2.size() - 1);
    }

    int lcs(int i1, int i2) override
    {
        return dp[i1][i2];
    }

private:

    std::string str1;
    std::string str2;
    vvi         dp;
    
    void CalcLcs()
    {
        for (int i = 0; i < str1.size(); ++i)
        {
            for (int j = 0; j < str2.size(); ++j)
            {
                if (i == 0 || j == 0)
                {
                    dp[i][j] = (str1[i] == str2[j]);

                    if (i == 0 && j == 0)
                        continue;

                    if (i == 0)
                        dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
                    else
                        dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
                }
                else
                    dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]) + (str1[i] == str2[j]);
            }
        }
    }
};

class LcsReccursion : public ILcs
{
public:
    LcsReccursion(const std::string& str1, const std::string& str2)
        : str1 {str1}
        , str2 {str2}
        , dp {vvi(str1.size(), std::vector<int>(str2.size(), nil))}
    {}

    int lcs() override
    {
        return lcs(str1.size() - 1, str2.size() - 1);
    }

    int lcs(int i1, int i2) override
    {
        if (dp[i1][i2] != nil)
            return dp[i1][i2];

        if (i1 == 0 || i2 == 0)
        {
            if (i1 == 0 && i2 == 0) return dp[i1][i2] = (str1[i1] == str2[i2]);
            else if (i1 == 0)       return dp[i1][i2] = std::max(lcs(i1, i2 - 1), (str1[i1] == str2[i2] ? 1 : 0));
            else                    return dp[i1][i2] = std::max(lcs(i1 - 1, i2), (str1[i1] == str2[i2] ? 1 : 0));
        }

        dp[i1][i2] = std::max(lcs(i1 - 1, i2), lcs(i1, i2 - 1))
            + (str1[i1] == str2[i2]);
        return dp[i1][i2];
    }

private:

    std::string str1;
    std::string str2;
    vvi         dp;
};

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    string s1, s2;

    for (int i = 0; i < n; ++i)
    {
        cin >> s1 >> s2;
        //LcsLoop lcs {s1, s2};
        LcsReccursion lcs {s1, s2};
        cout << lcs.lcs() << ((i != n - 1) ? "\n" : "");
    }
}

int main()
{
    solve();
    return 0;
}

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

using namespace std;

#define rep(i,x) for(int i=0;i<x;++i)
#define rrep(i,x) for(int i=x-1;i>=0;--i)

int dp[1010][1010];

int main()
{
    int q; cin >> q;

    rep(_, q) {
        string a,b; cin >> a>>b;

        int n = a.size(), m = b.size();

        rep(i, n) {
            rep(j, m) {
                if (a[i] == b[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[n][m] << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX_N 1000

int solve(std::string s,std::string t){
        int dp[MAX_N][MAX_N]={0};
        int n,m;
        n=s.size(),m=t.size();
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(s[i]==t[j])
                                dp[i+1][j+1]=dp[i][j]+1;
                        else
                                dp[i+1][j+1]=std::max(dp[i][j+1],dp[i+1][j\
]);
                }
        }
        return dp[n][m];
}

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

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

int main(){
  int n;
  cin >> n;
  for (int k=0;k<n;k++){
    string x,y;
    cin >> x >> y;
    int p=x.size();
    int q =y.size();
    int l[1000][1000] = {};
    for(int i=1;i<p+1;i++){
      for(int j=1;j<q+1;j++){
	if(x[i-1] == y[j-1] ){
	  l[i][j] = 1+l[i-1][j-1];
	}else{
	  l[i][j]= max(l[i-1][j],l[i][j-1]);
	}
      }
    }
    cout << l[p][q] << endl;
  }
}