#include <iostream>    
#include <fstream>
#include <sstream>
#include <algorithm>   
#include <vector>       
#include <map>       
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

using namespace std;

int main(int argc, char * argv[]){
    
	ifstream readR1file(argv[1]); 
	ifstream readR2file(argv[2]); 
	
	typedef map<string, string> mapType;
    mapType R1_SEQ_MAP;
    mapType R2_SEQ_MAP;

	string str="", key="", tmp_value="", value="";
	int r1_line_cnt = 0;
	while(getline(readR1file, str, '\n'))
	{                   
        if(r1_line_cnt<40000){
			r1_line_cnt++;
			vector<string> id_str;
			if(r1_line_cnt%4==1){	
				value = "";
				boost::split(id_str,str,boost::is_any_of("/"));
				key = id_str[0];
			}else{
				tmp_value = "";
				tmp_value = str + "\n";
				value += tmp_value;
			}
			if(r1_line_cnt%4==0){			
				R1_SEQ_MAP.insert(map<string, string>::value_type (key, value));
			}
		}
    }    
	readR1file.close();
	
	str="", key="", tmp_value="", value="";
	int r2_line_cnt = 0;
	while(getline(readR2file, str, '\n'))
	{                   
        r2_line_cnt++;
		vector<string> id_str;
		if(r2_line_cnt%4==1){	
			value = "";
			boost::split(id_str,str,boost::is_any_of("/"));
			key = id_str[0];
		}else{
			tmp_value = "";
			tmp_value = str + "\n";
			value += tmp_value;
		}
		if(r2_line_cnt%4==0){	
			R2_SEQ_MAP.insert(map<string, string>::value_type (key, value));
		}
    }    
	readR2file.close();
	
	ofstream writeR1file(argv[3]);
	ofstream writeR2file(argv[4]);

	//find
	map<string,string>::iterator iter = R1_SEQ_MAP.begin();
	for (iter=R1_SEQ_MAP.begin(); iter!=R1_SEQ_MAP.end(); ++iter){
		string find_key = iter->first;
		string r1_key = find_key + "/1";
		string r2_key = find_key + "/2";
		if(R2_SEQ_MAP.find(find_key) == R2_SEQ_MAP.end()){
			// not found
		}else{
			// found			
			writeR1file << r1_key << "\n" << iter->second;
			writeR2file << r2_key << "\n" << R2_SEQ_MAP.find(find_key)->second;
		}
	}
	writeR2file.close();   
	writeR2file.close();     

    R1_SEQ_MAP.clear();
    R2_SEQ_MAP.clear();
/*	
	map<string,string>::iterator it = R2_SEQ_MAP.begin();
	for (it=R2_SEQ_MAP.begin(); it!=R2_SEQ_MAP.end(); ++it)
		cout << it->first << " => " << it->second << '\n';
*/
  return 0;
}