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

题目大意、算法、思路:略。模板题,BKDRHash。

C++ Code:

 

#include
#include
using std::sort;
#define seed 233
int 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;
}

 

转载于:https://www.cnblogs.com/Mrsrz/p/6986046.html

你可能感兴趣的:([洛谷P3370]【模板】字符串哈希)