hash
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<cstring> #define inf 2147483647 #define For(i,a,b) for(register long long i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() #define MOD1 1e9+7 #define MOD2 1e9+9 #define base 31 //by war //2017.10.13 using namespace std; long long n; char c[10000]; struct has { long long one; long long two; bool operator<(const has&a)const { return one<a.one; } }ha[10010]; void in(long long &x) { long long y=1; char c=g();x=0; while(c<'0'||c>'9') { if(c=='-') y=-1; c=g(); } while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); x*=y; } void o(long long x) { if(x<0) { p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } long long hash(long long MOD) { long long ans=0; long long len=strlen(c); For(i,0,len-1) ans=(ans*base+(long long)c[i])%MOD; return ans; } int main() { long long ans=0; in(n); For(i,1,n) { scanf("%s",&c); ha[i].one=hash(MOD1); ha[i].two=hash(MOD2); } sort(ha+1,ha+n+1); For(i,2,n) if(ha[i].one!=ha[i-1].one||ha[i].two!=ha[i-1].two) ans++; o(++ans); return 0; }
一定要排序,让相同的都靠在一起,这样才可以判重。