
- #include
- #include
- #include
- using namespace std;
- using ull = unsigned long long;
- const int N = 1e4 + 9, P = 131;
- int n, ans = 1;
- char s[N];
- ull h[N], p[N], bis[N];
-
- ull get(char s[])
- {
- int len = strlen(s);
- for (int i = 1; i <= len; ++i)
- {
- h[i] = h[i - 1] * P + s[i];
- }
- return h[len];
- }
-
- int main()
- {
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- p[0] = 1;
- for (int i = 1; i <= N; ++i)
- {
- p[i] = p[i - 1] * P;
- }
- cin >> n;
- for (int i = 1; i <= n; ++i)
- {
- cin >> s + 1;
- bis[i] = get(s + 1);
- }
- sort(bis + 1, bis + n + 1);
- for(int i = 1; i < n; ++i)
- {
- if(bis[i] != bis[i + 1]) ans++;
- }
- cout << ans;
- return 0;
- }
此题与y总给的字符串哈希模板有些不同,仅仅只是多了一个数组来存储输入的每个字符串映射的哈希值。详情请看http://t.csdnimg.cn/7eWim
注意:在查找不同时,要先排序,ans初始化为1 。h[len]也就是字符串的哈希值,h[]数组是前缀哈希。