题解 | 字符串哈希
字符串哈希
https://www.nowcoder.com/practice/dadbd37fee7c43f0ae407db11b16b4bf
#include <bits/stdc++.h> using namespace std; unsigned long long h[15005]; unsigned long long P=131; int main() { int n; int cnt=1; cin>>n; memset(h,-1,sizeof h); bool ct=true; while(n--){ string s; ct=true; cin>>s; unsigned long long a=0; for(int i=0;i<s.size();i++){ a+=s[i]*pow(P,s.size()-i)+s[i]; } for(int i=1;i<=cnt;i++){ if(a==h[i]){ ct=false; break; } } if(ct==false) continue; h[++cnt]=a; } cout<<cnt-1<<endl; } // 64 位输出请用 printf("%lld")