Problem:
Search II

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output

Print C in a line.

Constraints

Elements in S is sorted in ascending order

n ≤ 100000

q ≤ 50000

0 ≤ an element in S ≤ 109

0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
1 2 3
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

Notes

Solution in C (judge verdict: Accepted)

#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;
}