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: Time Limit Exceeded)

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

void sort(int *, int);
int linear(int *, int*, int, int);
int same(int *, int);

int main() {
  int *s, *t;
  int n, q, i;

  scanf("%d", &n);
  s = (int *)malloc(n*sizeof(int));
  for(i=0;i<n;i++){
    if(scanf("%d", &s[i])==EOF)break;
  }
  sort(s, n);

  scanf("%d", &q);
  t = (int *)malloc(q*sizeof(int));
  for(i=0;i<q;i++){
    if(scanf("%d", &t[i])==EOF)break;
  }
  sort(t, q);

  printf("%d\n", linear(s, t, n, q));

  free(s);
  free(t);
  
  return 0;
}

void sort(int *A, int n){
  int i, j, m, temp;

  for(i=0;i<n;i++){
    m=i;
    for(j=i+1;j<n;j++){
      if(A[j]<A[m]) m=j;
    }

    if(i!=m){
      temp=A[i];
      A[i]=A[m];
      A[m]=temp;
    }
  }
}

int linear(int *s, int *t, int n, int q){
  int i, middle;
  int count = 0, flag=0, left, right;

  for(i=0;i<n;i++){
    left=0;
    right=q;
    while(left<right){
      middle = (left + right)/2;
      if(s[i]==t[middle] && same(s,i)==0) {
	count++;
	break;
      }
      else if(s[i]>t[middle]) left=middle+1;
      else right=middle;
      if(left-right>=0) break;
    }
  }
  return count;
}

int same(int *s, int k){
  int i;

  for(i=0;i<k;i++){
    if(s[i]==s[k]) return 1;
  }

  return 0;
}