https://www.luogu.com.cn/problem/P3370
#include <bits/stdc++.h> using namespace std; const int base1 = 29, base2 = 131, M = 2e5 + 5, mod1 = 1e9 + 7, mod2 = 1e9 + 9; int hash1[M], hash2[M], pow1[M], pow2[M]; string s; struct Hash { public: void init(string s) { // 下标从1开始 hash1[0] = hash2[0] = 0; pow1[0] = pow2[0] = 1; int n = s.size(); for (int i = 1; i <= n; ++i) { pow1[i] = pow1[i - 1] * base1 % mod1; pow2[i] = pow2[i - 1] * base2 % mod2; } for (int i = 1; i <= n; ++i) { hash1[i] = (hash1[i - 1] * base1 + s[i]) % mod1; hash2[i] = (hash2[i - 1] * base2 + s[i]) % mod2; } } pair<int, int> gethash(string s, int l, int r) { return {(hash1[r] - hash1[l - 1] * pow1[r - l + 1] % mod1 + mod1) % mod1, (hash2[r] - hash2[l - 1] * pow2[r - l + 1] % mod2 + mod2) % mod2}; } }; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; set<pair<int, int>> f; while (t--) { cin >> s; s = " " + s; int n = s.size(); Hash h; h.init(s); f.insert(h.gethash(s, 1, n - 1)); } cout << f.size(); return 0; }