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: Wrong Answer)

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

#define MAX 10000


/*バーブルソート*/
int BubbleSort ( int *Arr, int Max_order );

int BibarySearch( int *A, int Max_order, int key );

int main(void)
{
	int 	S[10000]				= {};
	char	str[30000000]				= {};
	int 	T[500]					= {};
	int 	n						= 0;
	int 	q						= 0;
	int 	i						= 0;
	int 	j						= i;
	int 	num						= 0;
	int     k                       = 0;
	fgets( str, 256, stdin );
	n = atoi(str);
	/*fgets( str, 20000000, stdin );
	while ( num < n ) {
		j = i;
		for ( i = 0; str[i] != ' ' && str[i] != '\r'; i++ ) {
		}
		str[i] = '\0';
		S[num] = atoi(&str[j]);
		num++;
		i++;
	}*/
	num = 0;
	while ( num < n-1 ) {
	    scanf("%d ", &S[num]);
	    num++;
	}
	scanf("%d\r", &S[num]);
	
	fgets( str, 256, stdin );
	q = atoi(str);
	
	num = 0;
	while ( num < q-1 ) {
	    scanf("%d ", &T[num]);
	    num++;
	}
	scanf("%d\r", &T[num]);
	/*
	fgets( str, 20000000, stdin );
	i = 0;
	num = 0;
	while ( num < q-1 ) {
		j = i;
		if ( num == 802 ) {
		    for ( k = 0; k < 10; k++ ) {
    		    printf("dump : %x\n", str[j+k]);
		    }
		}
		for ( i = 0; str[i] != 0x20; i++ ) {
		    if ( str[i] == '\r' || str[i] == '\n') {
		        break;
		    }
		}

		str[i] = '\0';
		if ( num == 802 ) {
		    for ( k = 0; k < 10; k++ ) {
    		    printf("xxdump : %x %x j-i:%d\n", str[j+k], ' ', j-i);
		    }
		}
		T[num] = atoi(&str[j]);
		if ( T[num] == 768 ) {
		    printf("num:%d error: %s\n", num ,&str[j]);
		}
		if ( num > 801 && num < 805) {
		    printf("num:%d error: %s value: %d \n", num ,&str[j], T[num]);
		}
		num++;
		i++;
	}
	
	printf("783: %d 803: %d\n", T[783], T[803]);
	*/

	/* バーブルソート */
//	BubbleSort( S, n );

	num = 0;
	for ( i = 0; i < q; i++ ) {
	    
		if ( 0 <= BibarySearch( S, n, T[i] ) ) {
			num++;
		} 
	}
	
	printf("%d\n", num );

	return 0;
}


/*バーブルソート*/
int BubbleSort ( int *Arr, int Max_order )
{
    int i = 0;		/* ループ 			*/
    int j = 0;		/* ループ 			*/
    int tmp = 0;	/* 入れ替えのダミー */
    
	
	/* メインループ */
    for ( i = 0; i < Max_order; i++ ) {
    	/* 一番後ろから手前のものと大きさを比べるループ */
        for ( j = Max_order - 1; j > i; j-- ) {
        	/* 大きさを比べて入れかえをする */
            if ( Arr[j] < Arr[j-1] ) {
                tmp = Arr[j];
                Arr[j] = Arr[j-1];
                Arr[j-1] = tmp;
            }
        }
    }
    
	
	
    return 0;
}

int BibarySearch( int *A, int Max_order, int key )
{
	int left = 0;
	int right = 0;
	int mid = 0;
	
	right = Max_order;
	while( left < right ) {
		mid = (left + right)/2;
		if ( A[mid] == key ) {
			return mid;
		} else if ( key < A[mid] ) {
			right = mid;
		} else {
			left = mid+1;
		}
	}
	
	return -1;
}