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 <cstdio>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <algorithm>
#define rep(i,n) for(int i=0; i<n; ++i)

using namespace std;
struct Doll{
    int H,R,L;
    bool operator<(const Doll& right)const {
        return H<right.H;
    }
    Doll(){
      H=0; R=0; L=0;
    }
};
int N, M;
int main(){
  while(cin >> N && N){
      vector<Doll> D;
      D.resize(200);
      rep(i,N){
        cin >> D[i+1].H >> D[i+1].R;
      }
      cin >> M;
      rep(i,M){
        cin >> D[N+i+1].H >> D[N+i+1].R;
      }
      sort(D.begin()+1, D.begin()+(N+M)+1);
      int ans = 0;
      for(int i=1; i<N+M+1; ++i){
        int m = 0;
        rep(j,i){
            if(D[j].R<D[i].R && D[j].H<D[i].H && m<D[j].L){
                m = D[j].L;
            }
        }
        D[i].L = m+1;
        ans = max(ans,D[i].L);
      }
      cout << ans << endl;
  }
}

C++: #include<iostream>
#include<cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 206
using namespace std;
int test=0;
int n,m;
int dp[MAX+1];
class Doll{
public:
   int h;
   int r;
   bool operator < (const Doll &d)const{
      if(h == d.h)return r < d.h;//hが同じ時ｒで比較
      return h < d.h;//普段はｈで比較
   }
};

Doll Dolls[MAX];

void solve(void){
   dp[0] = 0;
   //  rep(i,n+m){
   for(int i=0;i<(n+m);i++){
      dp[i+1] = 1;
      for(int j=0;j<i;j++)
	 if(Dolls[j].r < Dolls[i].r){
	    dp[i+1] = max(dp[i+1]+1,dp[j]+1);
      }
   }

   
   cout << "test" << ++test << endl;
   rep(i,n+m)
      cout << dp[i] << endl;
   cout << "ans" << endl;
   cout << dp[n+m] << endl << endl;
}

int main(void){
   int h,r;
   while(1){
      cin >> n;
      if(n==0)break;
      rep(i,n){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      cin >> m;
      for(int i=n;i<(n+m);i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      sort(Dolls,Dolls+n+m);
      rep(i,n+m)
	 cout << Dolls[i].h << "  " << Dolls[i].r<<endl;
      solve();

   }
   return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define ALL(c) (c).begin(), (c).end() 
using namespace std;

struct doll {
	int h, r; // h, r <= 1,000
	doll() {}
	bool operator<(const doll& right) const {
		return h == right.h ? r < right.r : h < right.h;
	}
	bool bigger_than(const doll d) {
		return h > d.h && r > d.r;
	}
};

#define MAX 100

int n, m; // n, m <= 100
vector<doll> dolls;
int dp[MAX * 2]; // dp[i]: i??????????????¢?????????????????£????????????

int main()
{	
	int maxval, i;
/************** input **************/
	while(cin >> n, n) {
		for(i = 0; i < n; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
		cin >> m;
		for(i = 0; i < m; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
/************** solve **************/
		sort(ALL(dolls));
#if 0
		for(int i = 0; i < n + m; i++)
			cout << dolls.at(i).h << " " << dolls.at(i).r << endl;
#endif
		for(i = 0; i < n + m; i++) {
			maxval = 0;
			for(int j = 0; j < i; j++) 
				if(dolls.at(i).bigger_than(dolls.at(j)))
					maxval = max(maxval, dp[j]);
			dp[i] = maxval + 1;
		}
/************** output **************/
		maxval = 0;
		for(i = 0; i < n + m; i++)
			maxval = max(maxval, dp[i]);
		cout << maxval << endl;
	}
}

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

int solve(map<int,set<int> > &M,int i,int cnt){
  int mx=0;
  if(M[i].empty())return cnt;
  for(set<int>::iterator it=M[i].begin();it!=M[i].end();++it){
    int t=solve(M,*it,cnt+1);
    if(mx<t)mx=t;
  }
  return mx;
}

int main(void){
  int n,m;
  while(cin>>n,n){
    vector<pair<int,int> > mat;
    mat.resize(n);
    for(int i=0,h,r;i<n;++i){
      cin>>h>>r;
      mat[i].first=h;
      mat[i].second=r;
    }
    cin>>m;
    mat.resize(m+n);
    for(int i=0,h,r;i<m;++i){
      cin>>h>>r;
      mat[n+i].first=h;
      mat[n+i].second=r;
    }
    map<int,set<int> > M;
    for(int i=0;i<m+n;++i){
      for(int j=m+n-1;j>i;--j){
        if(mat[i].first>mat[j].first && mat[i].second>mat[j].second){
          M[i].insert(j);
        }
        if(mat[j].first>mat[i].first && mat[j].second>mat[i].second){
          M[j].insert(i);
        }
      }
    }
    int ans=0;
    for(int i=0;i<m+n;++i){
      int a=solve(M,i,1);
      if(a>ans)ans=a;
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

struct Doll{
  int h,r;
  Doll(int a,int b){h=a;r=b;};
};

int n;
int h,r;
int m;
vector<Doll>doll;
int memo[201]={0};

bool comp(Doll a, Doll b){return a.h<b.h && a.r<b.r;};

int rec(int id)
{
  if(memo[id]!=0)return memo[id];

  int ans=0;
  for(int i=0;i<n+m;i++){
    if(comp(doll[i],doll[id]))ans=max(rec(i)+1,ans);
  }

  return memo[id]=ans;
}

int main()
{
  while(cin>>n,n){
    doll.clear();
    for(int i=0;i<200;i++)memo[i]=0;

    for(int i=0;i<n;i++){
      cin>>h>>r;
      doll.push_back(Doll(h,r));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>h>>r;
      doll.push_back(Doll(h,r));
    }

    doll.push_back(Doll(1001,1001));

    cout<<rec(n+m)<<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<n+m;j++){
        if(doll[i].first>doll[j].first && 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;
}