zoukankan      html  css  js  c++  java
  • P3370 【模板】字符串哈希

    luogu 传送门

    直接上板子

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring> 
    #define LL long long
    #define MOD1 1000000007
    #define MOD2 1000000009
    #define seed1 163
    #define seed2 131
    using namespace std;
    int n;
    struct OI{
        LL h1,h2;
    }H[10009];//开LL,用结构体!
    bool cmp(OI x,OI y)
    {return x.h1<y.h1;}
    LL hah(string x,int p)
    {
        int len=x.length();
        LL Hash=0;
        if(p==1)
        {
            for(int i=0;i<len;i++)
            Hash=(Hash*seed1+x[i]-'a')%MOD1;
            return Hash;
        }
        if(p==2)
        {
            for(int i=0;i<len;i++)
            Hash=(Hash*seed2+x[i]-'a')%MOD2;
            return Hash;
        }
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            string a;
            cin>>a;
            H[i].h1=hah(a,1);
            H[i].h2=hah(a,2);
        }
        sort(H+1,H+n+1,cmp);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(H[i].h1!=H[i-1].h1||H[i].h2!=H[i-1].h2) ans++;
        } 
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    [HAOI2016]食物链
    [TJOI2011]序列
    P2548 [AHOI2004]智能探险车
    [JSOI2008]最大数
    模板之dinic
    Excim的NOIP2017游记
    数列排序
    Car的旅行路线
    [HAOI2006]均分数据
    [luogu2210] Haywire
  • 原文地址:https://www.cnblogs.com/dfsac/p/7587828.html
Copyright © 2011-2022 走看看