P3370 [template] string hash solution to a problem

Address: https://www.luogu.org/problem/P3370

Seeking the number of different character strings  

    This problem can also be had with a set, but the big thing hash higher.

    Hash is actually a string mapped to a value, and these values ​​can not let the big probability repeat

    Binary hash: hash hex core band is given a fixed base , a string of numbers on each element considered as a binary bit, so this series can be seen as a base hex number, this number is the hash value,

    This problem is fixed by a conversion method, so that a different string of hash values ​​differ as much as possible. To avoid hash collision, select the appropriate binary, generally choose the maximum value is greater than the corresponding figures of all the characters, then mod a large prime number, and finally sentenced to heavy on it.

    

#include <iostream> 
#include <CString> 
#include <algorithm>
 a using  namespace std; 
typedef Long  Long LL;
 const  int MAXN 1E4 + = 19 ;
 const LL Base = 129 ;  
 const  Long  Long MOD = 212370440130137957ll; // I do not know why this use, remember the line 
LL has [MAXN]; 
char S [MAXN];
 int AC ( char * S) 
{ 
    int len = strlen (S); 
    LL SUM = 0 ;
    for(int i=0;i<len;i++)
    {
        sum=(sum*base+(ll)s[i])%mod;
    }
    return sum;
}
int main()
{
    ll t;
    scanf("%lld",&t);
    for(int i=0;i<t;i++)
    {
        scanf("%s",s);
        has[i]=ac(s);
    }
    sort(has,has+t);
    ll cnt=1;
    for(int i=0;i<t-1;i++)
    {
                if(has[i]!=has[i+1])
                cnt++;
    }
    printf("%lld\n",cnt);
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/liyexin/p/11583419.html