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 << dp[n+m][n+m]<< endl;


    }

    return 0;
}

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

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
typedef pair<int,int> pii;
int n,m;
pii a[222];
int dp[222];

inline bool le(pii const& t, pii const& u){
    return t.first<u.first && t.second<u.second;
}

int solve(){
    sort(a,a+n+m);
    dp[0]=1;
    for(int i=1;i<n+m;i++){
        for(int j=0;j<i;j++){
            if(le(a[j],a[i])){
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
    }
    int ans=0;
    for(int i=0;i<n+m;i++){
        ans=max(ans,dp[i]);
    }
    return ans;
}

int main(){
    while(cin>>n && n){
        rep(i,n)cin>>a[i].first>>a[i].second;
        cin>>m;
        rep(i,m)cin>>a[i+n].first>>a[i+n].second;
        cout << solve() << endl;
    }
}

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

#define pb push_back
#define mp make_pair

using namespace std;

int main(void)
{
	int n,m;
	int h,r;
	int i1,i2;
	int maxx;

	while(1){
		cin>>n;
		if(n==0) break;

		vector<pair<int,int> > doll;

		for(i1=0;i1<n;i1++){
			cin>>h>>r;
			doll.pb(mp(h,r));
		}
		cin>>m;
		for(i1=0;i1<m;i1++){
			cin>>h>>r;
			doll.pb(mp(h,r));
		}

		sort(doll.begin(), doll.end());
		vector<int> dp(doll.size(),1);
		maxx=0;
		
		for(i1=0;i1<doll.size();i1++){
			for(i2=0;i2<i1;i2++){
				if(doll[i1].first>doll[i2].first && doll[i1].second>doll[i2].second) {
					dp[i1]=max(dp[i1],dp[i2]+1);
				}
			}
			maxx=max(maxx,dp[i1]);
		}
		cout<<maxx<<endl;
	}
	return 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 dp[1001][1001] = {0};//h,r

int main() {

    while(1){

        for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){
                dp[i][j] = 0;
            }
        }

        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;
            dp[h][r] = 1;
            v.push_back(tmp);
        }

        int m;

        cin >> m;

        for(int i=0;i<m;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            dp[h][r] = 1;
            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());



        for(int k=0;k<n+m;k++) {
            for (int i = 1; i < v[k].h; i++) {
                for (int j = 1; j < v[k].r; j++) {
                    if (dp[i][j] != 0) {
                        dp[v[k].h][v[k].r] = max(dp[v[k].h][v[k].r], dp[i][j] + 1);
                    }
                }
            }
        }

        /*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[v[i].h][v[i].r],ans);
        }

        cout << ans << endl;


    }

    return 0;
}

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

typedef std::pair<int, int> P;

P data[202];
int dp[202];

int main(){

	int n, m;
	while (std::cin >> n, n){

		memset(data, 0, sizeof(data));
		memset(dp, 0, sizeof(dp));

		for (int i = 1; i <= n; i++){
			std::cin >> data[i].first >> data[i].second;
		}

		std::cin >> m;
		for (int i = 1; i <= m; i++){
			std::cin >> data[i + n].first >> data[i + n].second;
		}

		std::sort(data, data + 1 + n + m);

		for (int i = 1; i <= n + m; i++){
			for (int j = 0; j < i; j++){
				if (data[i].first > data[j].first&&data[i].second > data[j].second){
					dp[i] = std::max(dp[i], dp[j] + 1);
				}
			}
		}

		int max = 0;
		for (int i = 0; i <= n + m; i++){
			if (dp[i] > max)max = dp[i];
		}
		std::cout << max << std::endl;
	}
	return 0;
}