题解 | 字符串哈希

字符串哈希

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")

全部评论

相关推荐

07-18 14:03
门头沟学院 Java
点赞 评论 收藏
分享
Twilight_m...:还是不够贴近现实,中关村那块60平房子200万怎么可能拿的下来,交个首付还差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务