P4305【不重复数字】

不重复数字

题目

不重复数字


解析

感觉这题有黑
虽然只是一道哈希去重,但是32位有符号整数的范围就很灵性
快读快输也很必要

code:

#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll mod=50021;
bool isdigit(char x){
    
    return (x>='0'&&x<='9')?1:0;}
ll n,m,a[mod],b,t,l;
bool o;
inline int read()
{
    
    
	int num=0,f=1;
	char c=0;
	while(!isdigit(c=getchar())){
    
    if(c=='-')f=-1;}
	while(isdigit(c))num=(num<<1)+(num<<3)+(c&15),c=getchar();
	return num*f;
}
void write(long long x)
{
    
    
	(x<0)&&(x=-x,putchar('-'));
	if(x>9)write(x/10);
	putchar(x%10^48);
}
ll h(ll x)
{
    
    
	x=x%mod+1e9;
	x%=mod;
	return x;
}
ll wh(ll x)
{
    
    
	ll q=h(x),i=0;
	while(i<mod&&a[(q+i)%mod]!=x&&a[(q+i)%mod])i++;
	return (q+i)%mod;
}
int main()
{
    
    
	l=read();
	while(l--)
	{
    
    
		memset(a,0,sizeof(a));
		n=read();
		o=0;
		for(ll i=1;i<=n;i++)
		{
    
    
			b=read();
			if(!b)
			{
    
    
				if(!o)o=1;
				else continue;
			}
			if(a[wh(b)]==0)write(b),putchar(' ');
			a[wh(b)]=b;
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhanglili1597895/article/details/112973407