#include <stdio.h>
#include <stdlib.h>

#define MAX_SORT_DATA_NUM   100000
#define MAX_SEARCH_DATA_NUM  50000

#define FIND      1
#define NOT_FIND  0

typedef struct ST_BINARYDAtA{
    int  mid;       /* middle position */
    int  right;     /* right  position */
    int  left;      /* left   position */
}BinaryData_t;


static int BinarySerch(int *pSortBuffer, BinaryData_t *ptBinaryData, int search_value);


static int BinarySerch(int *pSortBuffer, BinaryData_t *ptBinaryData, int search_value)
{
	if(pSortBuffer[ptBinaryData->mid] == search_value)           /* find data */
	{
		return FIND;
	}
	else if(pSortBuffer[ptBinaryData->mid] > search_value )      /* Serch Value is left side */
	{
		
		if( ( ptBinaryData->left + 1 ) == ptBinaryData->mid )    /* No Data middle */
		{
			return NOT_FIND;
		}
		else
		{
			/* search left side data */
			ptBinaryData->right = ptBinaryData->mid;
			ptBinaryData->mid   = ((ptBinaryData->mid + ptBinaryData->left) / 2);
			
			return BinarySerch(pSortBuffer, ptBinaryData, search_value);
		}
	}
	else if(pSortBuffer[ptBinaryData->mid] < search_value )      /* Serch Value is right side */
	{
		
		if( ( ptBinaryData->right - 1 ) == ptBinaryData->mid )   /* No Data middle */
		{
			return NOT_FIND;
		}
		else
		{
			/* search right side data */
			ptBinaryData->left  = ptBinaryData->mid;
			ptBinaryData->mid   = ((ptBinaryData->mid + ptBinaryData->right) / 2);
			
			return BinarySerch(pSortBuffer, ptBinaryData, search_value);
		}
	}
	
	return NOT_FIND; /* error return */
}


int main(void)
{
	BinaryData_t tBinaryData;	/* BinaryData */
    int   sort_data_num;		/* Sort Data Num */
    int   search_data_num;		/* Search Data Num */
	int * pSortBuff;			/* Sort Buffer */
	int   search_value;			/* Search Value */
	int   find_count = 0;		/* Find Value Countor */
	int   loop = 0;
	
    /* get sort data num */
    scanf("%d", &sort_data_num);
    
	/* check Sort Data Num */
	if(sort_data_num <= MAX_SORT_DATA_NUM || sort_data_num == 0)
	{
        pSortBuff = (int *)malloc(sizeof(int) * sort_data_num);
	}
	else
	{
		/* error input or not data */
		printf("0\n");
		return 0;
	}
	
	/* get Sort Data */
    while( (loop < sort_data_num) && (scanf("%d", &(pSortBuff[loop])) != EOF) )
    {
    	loop++;
    }

	/* get search data num */
    scanf("%d", &search_data_num);
    
	/* check Search Data Num */
	if(search_data_num > MAX_SORT_DATA_NUM || search_data_num == 0)
	{
		/* error input or not data */
		printf("0\n");
		return 0;
	}

	/* init loop count */
	loop = 0;
	
    /* Search Data by Binary Search */
    while((scanf("%d", &search_value) != EOF) && loop < search_data_num)
    {
    	/* Set Binary Search Init Data */
    	tBinaryData.left  = 0;
    	tBinaryData.right = (sort_data_num - 1);
    	
    	if(sort_data_num > 1)
    	{
	    	tBinaryData.mid = (tBinaryData.right + tBinaryData.left) / 2;
    	}
    	else
    	{
    		tBinaryData.mid = pSortBuff[0];
    	}
    	
    	
		if( (search_value == pSortBuff[tBinaryData.left])   /* find Init Data */
		 || (search_value == pSortBuff[tBinaryData.right]) 
		 || (search_value == pSortBuff[tBinaryData.mid]) )
    	{
    		find_count++;
    	}
    	else if(sort_data_num > 3)               /* find Binary Search */
    	{
    		find_count += BinarySerch(pSortBuff, &tBinaryData, search_value);
    	}
    	else
    	{
    		/* Not Find */
    	}
    	
    	loop++;
    }
	
	/* print result */
	printf("%d\n", find_count);
    
    return 0;
}



Measured memory_bytes: 2068