P3370 [Template] String Hash

Topic description

For example, given N strings (the i-th string is of length Mi, the string contains numbers, upper and lower case letters, and is case sensitive), how many different strings are there in the N strings.

Input and output format

Input format:

 

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

The next N lines each contain one string, the provided string.

 

Output format:

 

The output contains one line containing an integer, the number of distinct strings.

 

Input and output example

Input example #1: 
5
abc
yyyy
abc
abcc
12345
Sample output #1: 
4

illustrate

Time and space limit: 1000ms, 128M

Data size:

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

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

For 100% data: N<=10000, Mi≈1000, Mmax<=1500

Example description:

In the example, the first string (abc) and the third string (abc) are the same, so the set of provided strings is {aaaa,abc,abcc,12345}, so there are 4 different strings in total .

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef unsigned long long ull;
 7 const int N=1e5+10,M=23333;
 8 int hs[N];
 9 ull hAsh(string a){
10     ull key=0;
11     int len=a.length();
12     for(int i=0;i<len;i++){
13         key=key*M+a[i];
14     }
15     return key;
16 }
17 int main (){
18     int n;
19     string a;
20     cin>>n;
21     for(int i=0;i<n;i++){
22         cin>>a;
23         hs[i]=hAsh(a);
24     }
25     sort(hs,hs+n);
26     int ans=1;
27     for(int i=1;i<n;i++){
28         if(hs[i]!=hs[i-1])ans++;
29     }
30     cout<<ans<<endl;
31 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378476&siteId=291194637