题意就是给 N 个字符串,求本质不同后缀的个数。
看了秒写哈希,一个WA on test 2,就两个,两个还不够就三个,为什么四个还WA on test 2? 求问。
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
set<tuple<ull,ull,ull,ull>> seen;
int main() {
mt19937_64 rng(time(0));
const ull Mod1=1e9+7;
const ull Mod2=1e9+9;
const ull Mod3=(1ull<<61)-1;
const ull Mod4=999999999999999989;
ull base1=rng();
ull base2=rng();
ull base3=rng();
ull base4=rng();
int n;
cin>>n;
for(int i=1;i<=n;i++) {
string s;
cin>>s;
ull h1=0,h2=0,h3=0,h4 = 0;
for (int j = s.size()-1; j >= 0; j--) {
ull c = s[j] - 'a';
h1=(h1*base1+c) % Mod1;
h2=(h2*base2+c) % Mod2;
h3=(h3*base3+c) % Mod3;
h4=(h4*base4+c) % Mod4;
seen.insert(make_tuple(h1, h2, h3, h4));
}
}
cout << seen.size() << endl;
return 0;
}