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++: ///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1001][1001];

static void init_dp(uint32_t len1, uint32_t len2)
{
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[str1.size()][str2.size()] << endl;
  }

  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int n, dp[1024][1024];
	string a, b;
	cin >> n;
	
	while(n)
	{
		n--;
		memset(dp, 0, sizeof(dp));

		cin >> a;
		cin >> b;

		for(int i = 0; i <= a.size(); i++)
		{
			for(int j = 0; j <= b.size(); j++)
			{
				if(a[i+1] == b[j+1])
				{
					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[a.size()][b.size()] << endl;
	}
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)

#define REP(i, n) FOR(i, 0, n)
#define REPE(i, n) for (int i = 0; i <= (n); ++i)

#define debug(x) cerr << x << " " \
                      << "(L:" << __LINE__ << ")" << '\n'

//DP法
template <class T>
inline bool chmin(T &a, T b) //change minimum
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) //change max
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

typedef long long ll; //64bit型
using namespace std;

const long long INF = 1LL << 60; //無限大の値
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 maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    REPE(i, m)
    {
        REPE(j, n)
        {
            C[i][j] = 0;
        }
    }
    FORE(i, 1, m)
    {
        FORE(j, 1, n)
        {
            if (X[i] == Y[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()
{
    string s1, s2;
    int n;
    cin >> n;
    REP(i, n)
    {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

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

using namespace std;

typedef long long ll;

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

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

int solve(const string &X, const string &Y) {
    const int xlen = (int) X.size();
    const int ylen = (int) Y.size();
    vector<vector<int>> dp(xlen + 1, vector<int>(ylen + 1, 0));

    for (int i = 1; i <= xlen; i++) {
        for (int j = 1; j <= ylen; j++) {
            if (X[i - 1] == Y[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[xlen][ylen];
}

int main(void) {
    int N;
    cin >> N;

    for (int i = 0; i < N; i++) {
        string X, Y;
        cin >> X >> Y;
        cout << solve(X, Y) << endl;
    }

    return 0;
}

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

int lcs(string X,string Y){
  int c[1001][1001];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for(int i=0;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 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-1][j],c[i][j-1]);
      maxl=max(maxl,c[i][j]);
    }
  }
  return maxl;
}

int main(){
  int q;cin>>q;
  while(q--){
    string a,b;cin>>a>>b;
    cout<<lcs(a,b)<<endl;
  }
  return 0;
}

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

int n;
string x, y;
bool mat[1000][1000];
int a[1000][1000];

int max(int i, int j) {
	int r = (!mat[i - 1][j] ? a[i - 1][j] : 0);
	r = ((!mat[i][j - 1] && a[i][j - 1] > r) ? a[i][j - 1] : r);
	return r > a[i - 1][j - 1] ? r : a[i - 1][j - 1];
}

int main(){
	cin >> n;
	for (int ia = 0; ia < n; ia++) {
		int ans = 0;
		cin >> x >> y;
		for (int i = 0; i < x.length(); i++) {
			mat[i][0] = x[i] == y[0] ? true : false;
			a[i][0] = mat[i][0] ? 1 : 0;
		}
		for (int i = 1; i < y.length(); i++) {
			mat[0][i] = x[0] == y[i] ? true : false;
			a[0][i] = mat[0][i] ? 1 : 0;
		}
		for (int i = 1; i < x.length(); i++) {
			for (int j = 1; j < y.length(); j++) {
				mat[i][j] = x[i] == y[j] ? true : false;
				a[i][j] = max(i, j) + (mat[i][j] ? 1 : 0);
			}
		}
		int p = 0;
		for (int i = 0; i < x.length(); i++) {
			p = a[i][y.length() - 1] > p ? a[i][y.length() - 1] : p;
		}
		for (int i = 0; i < y.length() - 1; i++) {
			p = a[x.length() - 1][i] > p ? a[x.length() - 1][i] : p;
		}
		cout << p << endl;
	}


	return 0;
}