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 <string>
#include <algorithm>
using namespace std;
//_____
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
//_______


int slv(string &a, string &b)
{
   const int alen = a.size();
   const int blen = b.size();
   int dp[1005][1005] = {};
   a = ' ' + a;
   b = ' ' + b;
   int ret = 0;

   reps(i, 1, alen){
      reps(j, 1, blen){
         if(a[i] == b[j]){
            dp[i][j] = dp[i-1][j-1] + 1;
         } else {
            dp[i][j] = max(dp[i-1][j] , dp[i][j-1] );
         }
         chmax(ret, dp[i][j]);
      }
   }
   return(ret);
}

signed main()
{
   FAST();
   int N;
   string a,b;
   cin >> N;
   rep(i, N){
      cin >> a >> b;
      outl( slv(a, b) );
   }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111
	
	int dp[3100][3100];

	
	void solve(){

		ini(n);
		while (n--)
		{
			ins(morty);
			ins(morty1);
			FOR(i,0,morty.size()+1)
			FOR(i1,0,morty1.size()+1)
			dp[i][i1] = 0;

			FOR(i,0,morty.size()+1)
			FOR(i1,0,morty1.size()+1){
				if(morty[i] == morty1[i1]){
					dp[i+1][i1+1] = dp[i][i1]+1;
				}
				else{
					dp[i+1][i1+1] = max(dp[i][i1+1],dp[i+1][i1]);
				}
			}
			out(dp[morty.size()][morty1.size()]);
		}
		
		

		


		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define MAX_LEN 1000

int dp[MAX_LEN + 1][MAX_LEN + 1];

int main() {
	int q;
	cin >> q;
	while (q-->0) {
		string X, Y;
		cin >> X >> Y;
		for (int i = 0; i <= X.size(); ++i)
			for (int j = 0; j <= Y.size(); ++j)
				dp[i][j] = 0;
		for (int i = 0; i < X.size(); ++i)
			for (int j = 0; j < Y.size(); ++j)
				if (X[i] == Y[j])
					dp[i+1][j+1] = dp[i][j] + 1;
				else
					dp[i+1][j+1] = max(dp[i][j + 1], dp[i + 1][j]);
		cout << dp[X.size()][Y.size()] << endl;
	}
	return 0;
}

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

int main(){
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        string x, y;
        cin >> x >> y;
        vector<vector<int>> dp(x.size()+1, vector<int>(y.size()+1, 0));
        
        for(int j = 1; j <= x.size(); j++){
            for(int k = 1; k <= y.size(); k++){
                if(x[j-1] == y[k-1]) dp[j][k] = dp[j-1][k-1]+1;
                dp[j][k] = max(dp[j][k], max(dp[j-1][k], dp[j][k-1]));
            }
        }

        cout << dp[x.size()][y.size()] << endl;
    }   
}

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 (max, 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 "bits/stdc++.h"

using namespace std;

// https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_10_C

int lcs(int currX, int currY, const string& X, const string& Y, vector<vector<int>>& memo) {
    if (currX == X.size() || currY == Y.size()) {
        return 0;
    }
    if (memo[currX][currY] >= 0) {
        return memo[currX][currY];
    }

    int ret;
    if (X[currX] == Y[currY]) {
        ret = lcs(currX + 1, currY + 1, X, Y, memo) + 1;
    }
    else {
        int moveX = lcs(currX + 1, currY, X, Y, memo);
        int moveY = lcs(currX, currY + 1, X, Y, memo);
        ret = max(moveX, moveY);
    }
    memo[currX][currY] = ret;
    return ret;
}

void Main() {
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        string X, Y;
        cin >> X;
        cin >> Y;

        vector<vector<int>> memo(1005, vector<int>(1005, -1));
        int ans = lcs(0, 0, X, Y, memo);
        cout << ans << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "DataStructure.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;
typedef pair<int, int> p;
const int INF = 1000000000;
int main(){
	int n; cin >> n;
	for (int k = 0; k < n; k++) {
		char s[1000], t[1000];
		scanf("%s",&s); scanf("%s", &t);
		int i = 0, j = 0;
		int dp[101][101];
		fill(dp[0], dp[101], 0);
		int res = 0;
		while (s[i] != '\0'){
			while (t[j] != '\0') 
			{
				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]);
				res = dp[i + 1][j + 1];
				//cout << "i " << i << "j " << j << endl;
				//cout << "dp" << dp[i + 1][j + 1] << endl;
				j++;
			}
			i++;
			j = 0;
		}

		cout << res << endl;
	}
	return 0;
}