刚学hash,有几个小问题(+_+)?

P3370 【模板】字符串哈希

emm...背 因为某些质数就是冲突小
by qseer @ 2018-10-24 20:07:53


@[qseer](/space/show?uid=68518) 我感觉那个自然溢出的hash很秀啊,什么都不用想,只要ans&INT_MAX..... 虽然不知道为什么可以这么做
by ioio0614 @ 2018-10-24 20:12:14


@[Reallyioio](/space/show?uid=92077) 取模的值您随便定啊(最好是大质数)
by Drinkkk @ 2018-10-24 20:18:17


@[Reallyioio](/space/show?uid=92077) 相加的素数您随便写
by Drinkkk @ 2018-10-24 20:18:48


@[Reallyioio](/space/show?uid=92077) 对,我就这么写的哈希,但有很多的版本 也推荐楼主用 unsigned long long 在超出的时候自动取模 ```c #include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> #include<set> using namespace std; set<long long> t; int n,base=131; long long a[10010]; char s[10010]; unsigned long long re; int hashe() { re=0; int len=strlen(s); for(int i=0;i<len;i++) re=re*base+s[i]; return re; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { memset(s,0,sizeof(s)); scanf("%s",s); t.insert(hashe()); } printf("%d",t.size()); } ```
by qseer @ 2018-10-24 20:29:22


|