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>
using namespace std;

int main(){
    int n = 0, m =0;
    int d[200][2], dp[200];
    
    for(int p = 0; true; p++){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
                d[i][0] = 0;
                d[i][1] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]){
                        if(d[j][1] < d[i][1]){
                            a = dp[j];
                        }
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}

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

struct DOLL{
	int h;
	int r;
};

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r)
{
	int ans = 0;
	for (DOLL &d : d1){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, d.h, d.r)+1;
			if( a<ans ) break;
			ans = a;
		}
	}
	for (DOLL &d : d2){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, d.h, d.r)+1;
			if( a<ans ) break;
			ans = a;
		}
	}
	return ans;
}

int main() {

	while (true){
		vector<DOLL> d1;
		vector<DOLL> d2;
		int n,m;

		cin >> n;
		if (n == 0) break;

		for (int i = 0; i<n; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d1.push_back(d);
		}

		cin >> m;

		for (int i = 0; i<m; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d2.push_back(d);
		}

		int ans = f(d1, d2, 0, 0);
		cout << ans << 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 sum=0;
    int max=1000;

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

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]++;
    }

    for(i=1000;i>0;i--)
      for(j=max;j>0;j--)
	if(doll[i][j]){
	  sum++;
	  max=j;
	  break;
	}

    cout<<sum<<endl;
  }
}

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

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		dp[0] = 1;
		for (int i = 1; i < n; ++i) {
			dp[i] = dp[i - 1];
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
					break;
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int dp[200] = {0};
int h[200];
int r[200];
int n,m;

int dfs(int id){
	if(dp[id])return dp[id];
	int ans = 1;
	rep(i,n){
		if(h[i] < h[id] && r[i] < r[id]){
			ans = max(dfs(i)+1,ans);
		}
	}
	return dp[id] = ans;
}
int main(){
	while(cin >> n , n){
		rep(i,200)dp[i] = 0;
		rep(i,n)cin >> h[i] >> r[i];
		cin >> m;
		rep(i,m)cin >> h[i+n] >> r[i+n];
		n += m;
		int ans = 0;
		rep(i,n){
			ans = max(ans,dfs(i));
		}
		cout << ans << endl;
	}
}

C++: // 0157 最長増加部分列
#include <iostream>
#include <algorithm>
#include <map>

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

using namespace std;
bool compare_h(pair<int, int> a, pair<int, int> b){
	return a.first < b.first;
}
bool compare_r(pair<int, int> a, pair<int, int> b){
	return a.second < b.second;
}


int main(){
	int n, m;
	while(cin >> n && n){
		int dp[256];
		// h, r
		pair<int, int> dolls[256];
		rep(i, n){
			cin >> dolls[i].first >> dolls[i].second;
		}
		cin >> m;
		rep(i, m){
			cin >> dolls[i+n].first >> dolls[i+n].second;
		}


		// hについてソートしrの最長増加部分列を求める
		int hlis = 0;
		sort(dolls, dolls+n+m, compare_h);
		dp[0] = 1;
		for(int i = 1; i < n+m; i++){
			dp[i] = 1;
			for(int j = 0; j < i; j++){
				if(dolls[j].second < dolls[i].second && dolls[j].first != dolls[i].first){
					dp[i] = max(dp[i], dp[j] + 1);
				}
			}
			hlis = max(hlis, dp[i]);
		}					

		cout << hlis << endl;
	}

	return 0;
}

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

struct Item{
  int h,r;
};

int main() {
  Item A[201];
  int L[201],P[201];
  int n; 
  while ( cin >> n, n ) {
    A[0].h = A[0].r = L[0] = 0;
    P[0] = -1;
    
    for ( int i = 1; i <= n; i++ ) cin >> A[i].h >> A[i].r;
    int m; cin >> m;
    for ( int i = n+1; i <= n+m; i++ ) cin >> A[i].h >> A[i].r;

    for (int i = 1; i <= n+m; i++ ) {
      for ( int j = 1; j < n+m; j++ ) {
	if ( A[j].h > A[j+1].h ) swap( A[j],A[j+1] );
      }
    }

 
    for ( int i = 1; i <= n+m; i++ ) {
      int k = 0;
      for ( int j = 0; j <= i-1; j++ ) {
	if ( A[j].h < A[i].h && A[j].r < A[i].r &&
	     L[j] > L[k] ) {
	  k = j;
	}
	L[i] = L[k] + 1;
	P[i] = k;

	}
      }
    int maxv = 0;
    for ( int i = 1; i <= n+m; i++ ) maxv = max ( maxv, L[i] );
    cout << maxv << endl;
    //  for ( int i = 1; i <= n+m; i++ ){
    //     cout << "L[i]" << " " << L[i] << endl;

    //  }
  }
  return 0;
}