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 <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
#include <ctime>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
#define DBG(X) cout<<#X<<" : "<<X<<endl;
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

int dp[210];

int main(){
    int n, m;
    int h, r;
    while(cin>>n && n){
        vector<pii> v;
        rep(i, n) {
            cin>>h>>r;
            v.pb(mp(h, r));
        }
        cin>>m;
        rep(i, m){
            cin>>h>>r;
            v.pb(mp(h, r));
        }
        sort(all(v));

        int ans = 0;
        for(int i = 0; i < 210; i++) dp[i]=1;
        for(int i = 1; i < n + m; i++){
            for(int j = 0; j < i; j++){
                if(v[j].fi < v[i].fi && v[j].se < v[i].se){
                    dp[i]=max(dp[j]+1, dp[i]);
                }
            }
            ans = max(ans, dp[i]);
        }
        cout<<ans<<endl;
    }  
}

C++: #include<bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main(){
    int n;
    while(cin >> n, n){
        vector <pair <int, int> > mato;
        int h, r;
        for(int i = 0; i < n; i++){
            cin >> h >> r;
            mato.push_back(make_pair(h, r));
        }
        int m;
        cin >> m;
        for(int i = 0; i < m; i++){
            cin >> h >> r;
            mato.push_back(make_pair(h, r));
        }
        sort(mato.begin(), mato.end());
        int sum[n + m] = {1}, ans = 0;
        for(int i = 1; i < n + m; i++){
            int macs= 0;
            for(int j = 0; j < i; j++){
                if(mato[i].first > mato[j].first && mato[i].second > mato[j].second)
                    macs = max(macs, sum[j]);
            }
            sum[i] = macs + 1;
            ans = max(ans, sum[i]);
        }
        cout << ans << endl;
    }
    
}

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

int main(){
	int n;
	while(cin >>n){
		if(n == 0) break;
		int mato[200][2], i, m;
		bool dp[200][200] = {false};
		for(i = 0; i < n; i++){
			cin >>mato[i][0] >>mato[i][1];
		}
		cin >>m;
		for(; i < (m + n); i++){
			cin >>mato[i][0] >>mato[i][1];
		}
		for(int j = 0; j < (n + m); j++){
			dp[0][j] = true;
		}
		for(int j = 1; j < (n + m); j++){
			for(int k = 0; k < (n + m); k++){
				if(dp[j - 1][k] == true){
					for(int l = 0; l < (n + m); l++){
						if(mato[l][0] > mato[k][0] && mato[l][1] > mato[k][1]){
							dp[j][l] = true;
						}
					}
				}
			}
		}
		
		/*for(int j = 0; j < (n + m); j++){
			cout <<j + 1 <<" : ";
			for(int k = 0; k < (n + m); k++){
				cout <<dp[j][k] <<" ";
			}
			cout <<endl;
		}*/
		int ans;
		bool flag = false;
		for(ans = (n + m - 1); ans >= 0; ans--){
			for(int k = 0; k < (n + m); k++){
				if(dp[ans][k] == true){
					flag = true;
					break;
				}
			}
			if(flag == true) break;
		}
		cout <<ans + 1 <<endl;
	}
	return 0;
}

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

struct D{int r, h;};

bool compr_as(const D& d1, const D& d2){
  return d1.r < d2.r;
}

bool comph_as(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[200];
  D dolls[200];

  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> dolls[i].h >> dolls[i].r;
    cin >> m;
    for(int i = n; i < n + m; i++) cin >> dolls[i].h >> dolls[i].r;

    int lis_r = 0, lis_h = 0;
    sort(dolls, dolls + n + m, compr_as);

    fill(dp, dp + n + m, 0);
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h < dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(dolls, dolls + n + m, comph_as);

    fill(dp, dp + n + m, 0);
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h < dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    cout << max(lis_h, lis_r) << endl;;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n,m;
  int i,j;
  int h,r;

  while(1){

    int doll[1005][1005]={};
    int dp[1005][1005]={};

    scanf("%d",&n);
    if(n==0)break;
    rep(i,n){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }
  
    for(i=1;i<1001;i++){
      dp[i][1]=dp[i-1][1];
      for(j=2;j<1001;j++){
	if(doll[i][j]){
	  dp[i][j]=dp[i-1][j-1]+doll[i][j];
	  // printf("i=%d j=%d doll=%d\n",i,j,dp[i][j]);
	  for(int k=j+1;k<1001;k++)
	    dp[i][k]=dp[i][j];
	  break;
	}
	dp[i][j]=max(dp[i-1][j],dp[i][j-1])+doll[i][j];
      }
    }
 
    cout<<dp[1000][1000]<<endl;
  }
}

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

using namespace std;

vector< pair<int,int> > doll;
int dp[200];
int n,m,tmp1,tmp2;

int main(){
  while(cin>>n && n){
    for(int i=0;i<n;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }

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

    int ans=0;

    for(int i=0;i<n+m;i++){
      dp[i]=1;
      for(int j=0;j<i;j++){
        if(doll[i].second>doll[j].second){
          dp[i]=max(dp[i],dp[j]+1);
        }
      }
      ans=max(ans,dp[i]);
    }

    cout<<ans<<endl;

    while(doll.size()) doll.pop_back();
    memset(dp,0,sizeof(dp));
  }
  return 0;
}