Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		hr := make([]entry, 0)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		sort.Sort(ByFirst(hr))
		fmt.Println(LIS(hr))
	}
}

type entry struct {
	first, second int
}

type ByFirst []entry

func(f ByFirst) Len() int {
	return len(f)
}

func (f ByFirst) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func (f ByFirst) Less(i, j int) bool {
	return f[i].first < f[j].first
}

func LIS(hr []entry) int {
	n := len(hr)
	lis := make([]int, n)
	lis[n-1] = 1
	for i := n-2; i > -1; i-- {
		m := 0
		for j := i+1; j < n; j++ {
			if hr[i].first < hr[j].first && hr[i].second < hr[j].second && lis[j] > m {
				m = lis[j]
			}
		}
		lis[i] = m+1
	}
	return lis[0]
}

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

struct doll{
    int h,r;
};

bool operator<(const doll& d1,const doll& d2){
    if(d1.h == d2.h){
        return d1.r < d2.r;
    }
    return d1.h < d2.h;
}

int main() {

    while(1){

        int n;

        cin >> n;

        if(n==0) break;

        int h,r;

        vector<doll> v;
        doll tmp;

        for(int i=0;i<n;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        int m;

        cin >> m;

        for(int i=0;i<m;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        /*for(int i=0;i<n+m;i++){
            cout << v[i].h << " " << v[i].r << endl;
        }*/

        sort(v.begin(),v.end());

        int table[201][201];

        for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                table[i][j] = 0;
            }
        }

        /*for(int i=0;i<n+m;i++){
            table[i][i] = 1;
        }*/

        for(int i=0;i<n+m;i++){
            for(int j=i;j<n+m;j++){
                if( (v[i].h<v[j].h && v[i].r < v[j].r) /*|| (v[i].h > v[j].h && v[i].r > v[j].r)*/ ){
                    table[i][j] = 1;
                    //table[j][i] = 1;
                }
            }
        }

        /*for(int i=0;i<n+m;i++){
            for(int j=0;j<n+m;j++){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/

        int dp[201][201] = {0};

        for(int i=0;i<=n+m;i++){
            dp[i][0] = 0;
            dp[0][i] = 1;
        }

        for(int i=1;i<=n+m;i++){
            for(int j=1;j<=n+m;j++){
                if(table[i-1][j-1]==1){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+1);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }

        }

        /*for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/

        int ans = 0;

        for(int i=0;i<=n+m;i++){
            //cout << dp[i] << endl;
            ans = max(dp[i][n+m],ans);
        }

        cout << ans << endl;


    }

    return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int INF = 10000000;
int main()
{
	int n;
	while(scanf("%d", &n), n){
		vector<pii> doll(n);
		rep(i, n)
			scanf("%d%d", &doll[i].first, &doll[i].second);
		
		int m;
		scanf("%d", &m);
		doll.resize(n+m);
		rep(i, m)
			scanf("%d%d", &doll[n+i].first, &doll[n+i].second);
		
		sort(doll.begin(), doll.end());
		vi dp(n+m, INF);
		rep(i, n+m){
			int id = distance(dp.begin(), lower_bound(dp.begin(), dp.end(), doll[i].second));
			dp[id] = doll[i].second;
		}
		
		int ans;
		for(int i=n+m-1; i>=0; --i){
			if(dp[i] != INF){
				ans = i + 1;
				break;
			}
		}
		
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);++i)
int main() {
  ios::sync_with_stdio(false);
  while(true) {
    int N; cin >> N;
    if(N == 0) break;
    vector<pair<int, int>> doll;
    REP(i,N) {
      int h, r; cin >> h >> r;
      doll.emplace_back(h, r);
    }
    int M; cin >> M;
    N += M;
    REP(i,M) {
      int h, r; cin >> h >> r;
      doll.emplace_back(h, r);
    }
    vector<vector<int>> G(N);
    REP(i,N) {
      REP(j,N) {
        if(doll[i].first > doll[j].first && doll[i].second > doll[j].second) {
          G[i].push_back(j);
        }
      }
    }
    vector<int> d(N, 1);
    while(true) {
      bool update = false;
      REP(i,N) {
        for(int next : G[i]) {
          if(d[next] < d[i] + 1) {
            d[next] = d[i] + 1;
            update = true;
          }
        }
      }
      if(!update) break;
    }
    cout << *max_element(d.begin(), d.end()) << endl;
  }
}

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

int N, M, h[200], r[200];

int main() {
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) {
            cin >> h[j] >> r[j];
        }
        cin >> M;
        for (int j = 0; j < M; ++j) {
            cin >> h[j+N] >> r[j+N];
        }

        int arr[1001][1001];
        for (int j = 0; j <= 100; ++j) fill(arr[j], arr[j] + 1001, 0);

        for (int j = 0; j < N+M; ++j) {
            arr[h[j]][r[j]] = 1;
        }

        int dp[1001][1001];
        for (int j = 0; j <= 100; ++j) fill(dp[j], dp[j] + 1001, 0);

        for (int j = 0; j <= 1000; ++j) {
            for (int k = 0; k <= 1000; ++k) {
                if (j > 0 && k > 0) {
                    dp[j][k] = max(dp[j-1][k-1] + arr[j][k], max(dp[j-1][k], dp[j][k-1]));
                } else if (j > 0) {
                    dp[j][k] = dp[j-1][k];
                } else if (k > 0) {
                    dp[j][k] = dp[j][k-1];
                }
            }
        }

        cout << dp[1000][1000] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int n, m, ans;
pair<int, int> doll[200];
int dp[1000][1000];
int main()
{
	while(cin >> n && n)
	{
		memset(dp, -1, sizeof(dp));
		ans = 0;
		dp[0][0] = 0;
		for(int i = 0; i < n; i++)
			cin >> doll[i].F >> doll[i].S;
		cin >> m;
		for(int i = n; i < n + m; i++)
			cin >> doll[i].F >> doll[i].S;
		sort(doll, doll + n + m);
		for(int i = 0; i < n + m; i++)
			for(int j = 0; j < doll[i].F; j++)
				for(int k = 0; k < doll[i].S; k++)
					dp[doll[i].F][doll[i].S] = max(dp[doll[i].F][doll[i].S], dp[j][k] + 1);

		for(int i = 0; i < 1000; i++)
			for(int j = 0; j < 1000; j++)
				ans = max(ans, dp[i][j]);
			cout << ans << endl;
	}

}