CDOJ 1092 韩爷的梦 字符串哈希

来源:互联网 发布:全宋词软件下载 编辑:程序博客网 时间:2024/05/19 18:38

就是字符串哈希求个数,因为内存限制很小,所以只能把所有的哈希值存下来,然后排序,去重,就可以

不知为什么,我的p选的是1e9+7,mod是1e9+9,就会WA,然后两个换一下。p是1e9+9,mod是1e9+7,就过了,2333

原来字符串哈希也不难。。。。去年的时候,,,还是十分懵逼的小白。。。。什么都看不懂。。。

代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <string>#include <algorithm>using namespace std;#define maxn 105#define p 1000000009#define mod 1000000007#define num 20000char ch[maxn];int cnt[num];long long hashi[maxn];int idx[260];void init_idx(){for (char i = 'a'; i <= 'z'; ++i)idx[i] = i - 'a' + 1;for (int i = 'A'; i <= 'Z'; ++i)idx[i] = i - 'A' + 27;for (int i = '0'; i <= '9'; ++i)idx[i] = i - '0' + 53;}int main(){//freopen("input.txt", "r", stdin);int ans = 0, n;init_idx();for (int i = 0; i < num; ++i){scanf("%s", ch);n = strlen(ch);for (int j = 1; j <= n; ++j)hashi[j] = (hashi[j - 1] * p + idx[ch[j - 1]]) % mod;cnt[i] = hashi[n];}sort(cnt, cnt + num);printf("%d\n", unique(cnt, cnt + num) - cnt);//while (1);return 0;}

0 0
原创粉丝点击