二次hash字符串

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const LL D1=131;
const LL D2=131;
const LL MOD1=1e9+7;
const LL MOD2=1e9+9; 
char ss[10010];
struct node{
    LL hash1,hash2;
}a[10011];
LL make_hash1(char s[])
{
    LL ans=0;
    for(int i=0;i<strlen(s);i++)
     ans=(ans*D1+(LL)(s[i]))%MOD1;
    return ans; 
}
LL make_hash2(char s[])
{
    LL ans=0;
    for(int i=0;i<strlen(s);i++)
     ans=(ans*D2+(LL)(s[i]))%MOD2;
    return ans; 
}
bool comp(const node&a,const node&b)
{
    return a.hash1<b.hash1;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
     scanf("%s",ss),a[i].hash1=make_hash1(ss),a[i].hash2=make_hash2(ss);

    sort(a+1,a+n+1,comp);
    int ans=1;
    for(int i=2;i<=n;i++)
     if(a[i].hash1!=a[i-1].hash1||a[i].hash2!=a[i-1].hash2) ans++;
    printf("%d",ans);
    return 0; 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值