【STL】{A}∩{B}

描述

Given two integer sets A and B sorted descendingly, you are asked to output the element count of A∩B.

输入

Standard input will contain multiple test cases.
The first line of the input is a single integrate T (1 <= T <= 50) which is the number of test cases. then T consecutive test cases followed.
In each test case, there has four lines.
The first line contains the element count N(1<=N<=100000) for the first set.
The second line contains N integers in the first set.
The third line contains the element count M(1<=M<=100000) for the second set.
The fourth line contains M integers in the second set.

NOTE: there will be no duplicate elements in each sets and all the integers in each set have been sorted descendingly.

输出

For each test case in the input, there’s only one line output that contains the element count of the intesection of the two sets.

样例输入

1
5
5 4 3 2 1
4
5 3 1 -1

样例输出

3

扫描二维码关注公众号,回复: 8737104 查看本文章

复习:
ios::sync_with_stdio(false); 减少cin的时间消耗。

代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
int N,M;
while(T–)
{
int m;
map<int,int> num;
cin>>N;
for (int i=0;i<N;i++)
{
cin>>m;
num[m]++;
}
cin>>M;
int ans=0;
for (int i=0;i<M;i++)
{
cin>>m;
if (num[m]) ans++;
}
cout<<ans<<endl;
}
return 0;
}

发布了65 篇原创文章 · 获赞 0 · 访问量 1319

猜你喜欢

转载自blog.csdn.net/Skynamer/article/details/104055305