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 <bits/stdc++.h>

using namespace std;

int dp[1010][1010];

int main(void) {
    int q;
    string x, y;
    cin >> q;

    while (cin >> x >> y) {
        memset(dp, 0, sizeof dp);

        int xs = x.size(), ys = y.size();
        for (int i = 1; i <= xs; i++)
            for (int j = 1; j <= ys; 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], max(dp[i][j - 1], dp[i - 1][j - 1]));
            }
            
        cout << dp[xs][ys] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;
 
using namespace std;
int dp[1005][1005];
 
int main() {
    int n; cin >> n;
    REP(k, n){
        REP(i, 1005) REP(j, 1005) dp[i][j] = 0;
        string a, b;
        cin >> a >> b;
        REP(i, a.length()){
            string q = a.substr(i, 1);
            REP(j, b.length()){
                string t = b.substr(j, 1);
                if(q == t){
                    if(i == 0 || j == 0)
                        dp[i][j] = 1;
                    else
                        dp[i][j] = (dp[i-1][j] > dp[i][j-1]) ? max(dp[i-1][j-1] + 1, dp[i-1][j]) : max(dp[i-1][j-1] + 1, dp[i][j-1]);
                }
                else if (i != 0 && j != 0)
                    dp[i][j] = (dp[i-1][j] > dp[i][j-1]) ? dp[i-1][j] : dp[i][j-1];
                else if (i != 0)
                    dp[i][j] = dp[i-1][j];
                else
                    dp[i][j] = dp[i][j-1];
            }
        }
        cout << dp[a.length()-1][b.length()-1] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;
int dp[1000][1000];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;
	cin>>n;
	string s1,s2;
	while(n--){
		cin>>s1>>s2;
		REP(i,s1.size())dp[i][0] = 0;
		REP(i,s2.size())dp[0][i] = 0;
		FOR(i,1,s1.size()+1){
			FOR(j,1,s2.size()+1){
				if(s1[i] == s2[j])dp[i][j] = dp[i-1][j-1]+1;
				else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout << dp[s1.size()][s2.size()] << 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 maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    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 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 = C[i][j];
        }
    }
    return maxl;
}


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 <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(){
  int N,X,Y;
  char x[1024],y[1024];
  int dp[1024][1024];
  scanf("%d",&N);
  while(N--){
    scanf("%s%s", x, y);
    X = strlen(x); Y = strlen(y);
    memset(dp, 0, sizeof dp);
    for(int i=0;i<X;i++){
      for(int j=0;j<Y;j++){
	if(x[i]==y[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]);
      }
    }
    printf("%d\n", dp[X][Y]);
  }
  return 0;
}

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

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

const int N = 1000;

int main() {
  int q, lmax;
  int c[N + 1][N + 1] = {0};
  string X, Y;

  cin >> q;
  REP(i, q) {
    cin >> X >> Y;

    X = ' ' + X;
    Y = ' ' + Y;
    lmax = 0;
    REP(i, X.length()) REP(j, Y.length()) {
      if (i == 0 || j == 0) continue;
      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]);
      }
      lmax = max(lmax, c[i][j]);
    }

    cout << lmax << endl;
  }

  return 0;
}

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

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

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		string a, b;
		cin >> a >> b;

		for (int i = 1; i <= a.size(); i++)
		{
			for (int j = 1; j <= b.size(); j++)
			{
				if (a[i - 1] == b[j - 1])
					memo2[j] = max({ memo2[j - 1], memo1[j], memo1[j - 1] + 1 });
				else
					memo2[j] = max(memo2[j - 1], memo1[j]);
			}
			swap(memo1, memo2);
			memo2[0] = 0;
		}

		cout << memo1[b.size()] << endl;
	}

	return 0;
}

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

int main() {
    int q;
    cin >> q;
    while (q--) {
        string X, Y;
        cin >> X >> Y;
        int dp[X.size() + 1][Y.size() + 1];
        memset(dp, 0, sizeof(dp));
        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 + 1][j], dp[i][j + 1]);
                }
            }
        }
        cout << dp[X.size()][Y.size()] << endl;
    }

    return 0;
}

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

#define NUM 1000

string X;
string Y;
int dp[NUM + 1][NUM + 1];

void init(){
    for(int i = 0; i < NUM + 1; i++){
        for(int j = 0; j < NUM + 1; j++){
            dp[i][j] = 0;
        }
    }
}

void calc(void){
    init();

    for(int j = 1; j <= Y.size(); j++){
        for(int i = 1; i <= X.size(); i++){
            if(Y[j] == X[i]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }else{
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    
    printf("%d\n", dp[X.size()][Y.size()]);

}

int main(void){
    int q;
    scanf("%d", &q);

    for(int i = 0; i < q; i++){
        cin >> X;
        cin >> Y;
        calc();
    }
}

C++: #include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int solve(std::string x, std::string y) {
  int m = x.length();
  int n = y.length();
  int lcs[m + 1][n + 1];
  rep(i, m + 1) lcs[i][0] = 0;
  rep(j, n + 1) lcs[0][j] = 0;

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

  return lcs[m][n];
}

int main() {
  int n;
  std::cin >> n;
  std::string x, y;

  rep(i, n) {
    std::cin >> x >> y;
    std::cout << solve(x, y) << '\n';
  }
  return 0;
}