Keyword: ad hoc
Algorithm:
Use a Boolean array to record the CD they have.
If an ID is met twice, add 1 to the answer.
Judge's memory is sufficient enough to contain an 1GB array.
Remember to use malloc or new to get such large memory.
Code:
#include<cstdio>
int main()
{
int a, b, tmp;
while( scanf( "%d %d", &a, &b ) && ( a+b ) )
{
int both = 0;
bool *has = new bool[ 1000000001 ];
for( int i = a+b; i; --i )
{
scanf( "%d", &tmp );
if( has[ tmp ] )
++both;
else
has[ tmp ] = true;
}
printf( "%d\n", both );
delete[] has;
}
return 0;
}
沒有留言:
張貼留言