BZOJ2761 不重複數字,bzoj2761重複數字
題目Description
給出N個數,要求把其中重複的去掉,只保留第一次出現的數。
例如,給出的數為1 2 18 3 3 19 2 3 6 5 4,其中2和3有重複,去除後的結果為1 2 18 3 19 6 5 4。
Input
輸入第一行為正整數T,表示有T組資料。
接下來每組資料包括兩行,第一行為正整數N,表示有N個數。第二行為要去重的N個正整數。
Output
對於每組資料,輸出一行,為去重後剩下的數字,數字之間用一個空格隔開。
Sample Input
2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6
Sample Output
1 2 18 3 19 6 5 4
1 2 3 4 5 6
HINT
對於30%的資料,1 <= N <= 100,給出的數不大於100,均為非負整數;
對於50%的資料,1 <= N <= 10000,給出的數不大於10000,均為非負整數;
對於100%的資料,1 <= N <= 50000,給出的數在32位有符號整數範圍內。
代碼hash
蒟蒻至今不會寫hash,只能用stl水過
#include<cstdio>#include<bits/stdc++.h>#include<cstring>#include<cstdlib>#define R0(i,n) for(int i=0;i<n;++i)using namespace std;typedef long long ll;int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int main(){ int t=read(); while(t--){ int n=read(); set<int>S; vector<int>ans; R0(i,n){ int tmp=read(); if(S.count(tmp))continue; S.insert(tmp); ans.push_back(tmp); } R0(i,ans.size()-1)printf("%d ",ans[i]); printf("%d\n",ans[ans.size()-1]); } return 0;}