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;

string X, Y;
int M[1001][1001];

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> X >> Y;
		for (int i = 0; i <= X.length(); i++) {
			for (int j = 0; j <= Y.length(); j++) {
				M[i][j] = 0;
			}
		}
		for (int i = 0; i < X.length(); i++) {
			for (int j = 0; j < Y.length(); j++) {
				if (X[i] == Y[j]) M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i][j] + 1);
				M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i + 1][j]);
				M[i + 1][j + 1] = max(M[i + 1][j + 1], M[i][j + 1]);
			}
		}
		cout << M[X.length()][Y.length()] << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include <string.h>
#include <algorithm>

using namespace std;

int main(){
  int N;cin>>N;
  string X[N], Y[N];
  for (int i=0;i < N; i++) {
    cin>>X[i];cin>>Y[i];
  }
  for (int i=0;i < N; i++) {
    string x_t=X[i];int x_len=x_t.length();
    string y_t=Y[i];int y_len=y_t.length();
    int dp[x_len+1][y_len+1];
    memset(dp,0,sizeof(dp));
    for (int x = 0; x <= x_len; x++)dp[x][0]=0;
    for (int y = 0; y <= y_len ; y++)dp[0][y]=0;
    for (int x=1;x<=x_len;x++){
      for (int y=1;y<=y_len;y++){
        if(x_t[x-1]==y_t[y-1]){
          dp[x][y]=dp[x-1][y-1]+1;
        }else{
          dp[x][y]=max(dp[x][y-1],dp[x-1][y]);
        }
      }
    }
    cout<< dp[x_len][y_len] << "\n";
  }

}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
  
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
  
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A);
strB = strlen(B);
 
    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>

using namespace std;

#define ARRAY_MAX 150
#define MAX 1000 + 1

char X[MAX];
char Y[MAX];

#pragma region 暴力递归
int FindLCS1(char x[], int i, char y[], int j)
{
	int l1 = strlen(x);
	int l2 = strlen(y);

	// 到索引末尾结束
	if (i >= l1 || j >= l2) return 0;

	if (x[i] == y[j])
		return 1 + FindLCS1(x, i + 1, y, j + 1);
	else
		return max(FindLCS1(x, i, y, j + 1), FindLCS1(x, i + 1, y, j));
}

int LongestCommonSubsequence1(char x[], char y[])
{
	return FindLCS1(x, 0, y, 0);
}
#pragma endregion

#pragma region 记忆搜索
int memory[MAX][MAX];
int FindLCS2(char x[], int i, char y[], int j)
{
	int l1 = strlen(x);
	int l2 = strlen(y);

	// 到索引末尾结束
	if (i >= l1 || j >= l2) return 0;

	if (memory[i][j] != -1) return memory[i][j];

	if (x[i] == y[j])
		return memory[i][j] = 1 + FindLCS2(x, i + 1, y, j + 1);
	else
		return memory[i][j] = max(FindLCS2(x, i, y, j + 1), FindLCS2(x, i + 1, y, j));
}

int LongestCommonSubsequence2(char x[], char y[])
{
	int l1 = strlen(x);
	int l2 = strlen(y);
	for (int i = 0; i < l1; ++i)
		for (int j = 0; j < l2; ++j)
			memory[i][j] = -1;

	return FindLCS2(x, 0, y, 0);
}
#pragma endregion

#pragma region 动态规划
int dp[MAX][MAX];

int LongestCommonSubsequence3(char x[], char y[])
{
	int l1 = strlen(x);
	int l2 = strlen(y);
	int size = sizeof(int) * (MAX) * (MAX);
	memset(dp, 0, size);
	for (int i = l1 - 1; i >= 0; --i)
		for (int j = l2 - 1; j >= 0; --j)
		{
			if (x[i] == y[j])
				dp[i][j] = 1 + dp[i + 1][j + 1];
			else
				dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
		}

	return dp[0][0];
}
#pragma endregion

int main()
{
	int q; cin >> q;
	queue<int> result;
	for (int i = 0; i < q; ++i)
	{
		cin >> X;
		cin >> Y;
		result.push(LongestCommonSubsequence3(X, Y));
	}

	while (!result.empty())
	{
		cout << result.front() << endl;
		result.pop();
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
char map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	int val;

	if(x < 0 || y < 0) return 0;

	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			val = findMaxLength(x-1,y-1) +1;
		}else{
			val = max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
		map[x][y] = val;//メモ
		return val;
	}else return map[x][y];
}

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

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<endl;
	}

	return 0;
}

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

using namespace std;



int n;
string a, b;
string x[1000], y[1000];




int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        
        if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001][1001];
char x[1001], y[1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		scanf("%s%s", x, y);
		rep(i, 1000)rep(j, 1000) {
			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]);
		}
		printf("%d\n", dp[1000][1000]);
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

template<class T> inline void chmax(T& a, T b) { if (a < b) a = b; }
template<class T> inline void chmin(T& a, T b) { if (a > b) a = b; }


namespace Lcs
{

	int exec()
	{
		string S, T; cin >> S >> T;
		vector<vector<int>> dp(S.size() + 1, vector<int>(T.size() + 1, 0));
		for (int s = 0; s < S.size(); ++s) {
			for (int t = 0; t < T.size(); ++t) {
				if (S[s] == T[t]) dp[s + 1][t + 1] = dp[s][t] + 1;
				chmax(dp[s + 1][t + 1], dp[s + 1][t]);
				chmax(dp[s + 1][t + 1], dp[s][t + 1]);
			}
		}
		return dp[S.size()][T.size()];
	}
}

int main()
{
	int Q; cin >> Q;
	vector<int> anss(Q);
	for (int i = 0; i < Q; ++i) {
		anss[i] = Lcs::exec();
	}
	for (int i = 0; i < Q; ++i) {
		cout << anss[i] << endl;;
	}
}