Getting started hashing algorithm (algorithm contains only contest section)

Hash Algorithm

  Hash algorithm is a string algorithms, hash essence, there are many, but the algorithm competitions often only relates to a forthcoming referred to as a k a string of hexadecimal numbers, to ensure that each length, each content the only way to have a string expression in the k-ary, hash algorithm itself is to provide a string of abbreviated way, this way can often make string comparison even more convenient and fast, we can hash as a function, we will enter into a string function, then the value is another kind of (only) the expression of the string returned.

  Here we detail the hash algorithm.

  Hash algorithms generally comprise the following steps: a fixed binary (i.e., provided a band k, k may ensure that every possible character cover and the value will not be repeated) 2 modulo number (after modulo not guaranteed. conflict)

  Manner calculated hash value string is generally recursive manner, such as a string 'asdasdac' string by lowercase letters, we can take the modulo 27, this as a string of 27 hexadecimal numbers, we read the first character 'a' we define a count variable to the value of the string of record: the sum, we read the first character, sum = sum * 27 + 'a'; Next, we continue to read the second character: 's' we sum = sum * 27 + 'c' in this case, the value is stored us 'a' * 27 + 'c' that It happens to be a two-digit hexadecimal number 27, each multiplied by the weight of this bit. We can get this down recursive hash value of the string, if we compare two strings are equal when you can directly compare the hash value of the two strings are equal, then the two strings are equal if must be equal.

  Let me put a question hash entry

Title Description

As stated, a given string of N (i-th length of the string Mi, comprising the string numbers, uppercase and lowercase letters, case sensitive), the request number of different strings of string N total.

# Friendly reminder: If you really want a good practice hash, please consciously, otherwise turn right PJ field trials :)

Input Format

The first row contains an integer N, is the number of strings.

Next N lines contains a string, a string provided.

Output Format

Comprising an output line, comprising an integer, for the different number of strings.

Sample input and output

Input # 1
5
abc
aaaa
abc
abcc
12345
Output # 1
4

Description / Tips

Constraints of time: 1000ms, 128M

Data Scale:

For 30% of the data: N <= 10, Mi≈6, Mmax <= 15;

For 70% of the data: N <= 1000, Mi≈100, Mmax <= 150

To 100% of the data: N <= 10000, Mi≈1000, Mmax <= 1500

Sample Description:

A first sample string (ABC) and the third string (ABC) is the same, it is provided a set of strings {aaaa, abc, abcc, 12345}, so that a total of four different string .

 

## topics from Los Valley Title Author HansBug

 

  We look at the questions, we only find the value of each string, and then sort sort a hash value for each judge and after a hash values ​​are equal, if not equal, the answer is a plus.

On the following codes:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
#define unlolo unsigned long long
using namespace std;
unlolo a[50000],n;
char d[50000];
unlolo b=131,l;
bool cmp(int x,int y){
    return x<y;
}
unlolo hash(char s[]){
    unlolo ans=0;
    for(int i=0;i<strlen(s)-1;i++){
        ans=ans*b+(unlolo)s[i];
    }
    return ans;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>d;
        a[i]=hash(d);
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++){
        if(a[i]!=a[i+1 ]) l ++ ; 
    } 
    Cout << l; 
}

thanks for reading.

Guess you like

Origin www.cnblogs.com/tianbowen/p/11349949.html