首页 > 代码库 > [洛谷P3370]【模板】字符串哈希

[洛谷P3370]【模板】字符串哈希

题目大意、算法、思路:略。模板题,hash不多解释。

C++ Code:

 

#include<cstdio>#include<algorithm>using std::sort;#define seed 233int n;char s[1555];unsigned int a[10005];inline unsigned int hash(char* s){	int h=0;	while(*s++)h=h*seed+(*s);	return h%0x7fffffff;}int main(){	scanf("%d",&n);	for(int i=1;i<=n;i++){		scanf("%s",s);		a[i]=hash(s);	}	sort(a+1,a+n+1);	a[n+1]=-1;	int ans=0;	for(int i=1;i<=n;i++)	if(a[i]!=a[i+1])ans++;	printf("%d\n",ans);	return 0;}

 

  

 

[洛谷P3370]【模板】字符串哈希