[String] elegant violence - hash sentenced to heavy issues in a string

  Often encounter problems when sentenced to heavy brush title / game, then this problem would be to talk about the heavy sentence on a string.

 

What is hash ▎

  Heavy sentence what we usually think of? Xiao Bian first thought was that bucket sort, this sort it is with the hash method, in fact, the hash understood as a bunch of barrels more appropriate.

  For example, now give you a bunch of numbers, let you judge a total of several digital (that is, not recurring): 15,411,356. To hash ideas to solve is this:

  

  Put a number of buckets, each bucket represents a number, the corresponding number on the face into them, judge several digital is transformed into something the judge has several barrels of it.

  So, the next think about a problem: how to keep these barrels? To keep these barrels as long as the amount of recurring absolutely impossible to represent the number of barrels, such as ...... array index! We can use the array subscripts to as buckets, each bucket of what the number is a value corresponding to the array elements. For example, using an array called a bucket to keep these, when faced with numbers 3, as long as a [3] ++; on it.

  In fact, this is a hash, so that understanding into a pile of barrels more vivid.

 

Under the hash string ▎

  See here, you will want, hash strings have nothing to talk about? Is not a truth? Of course not! Think about it, how to store a string array subscript it? Array subscript are integers, ah!

  At this way it is very light and spacious, and we can convert a string to an integer processing!

  do you remember? Learned ASCII code in the beginning to learn Shihai, we can be replaced by an integer cast.

  But the problem again, how to represent the string with ASCII? E.g. ASCII code AB, where A is 65, B is 66 ASCII code.

  1) added: AB is expressed as 65 + 66 = 131. Anti Example: BA is expressed as 66 + 65 = 131, AB and BA may be not the same;

  2) using subtraction, multiplication, addition, and seems supra, shows a value not unique;

  3) put together: AB is expressed as 6566, so really could not give any counter-examples, but the digital value becomes larger, but also distinguish not go back, whether it is 6566 and 6 566 it? Or 6,56 and 6 it? It does not seem to know the original look like a string.

  Naturally, we think the band turn, this problem is not easy. So what kind of band would take less problems occur not happen? We tend to take 27,233,19260817, etc., as the case may be specific. (Example will explain later).

  Sometimes out of range of unsigned long long, then how to do it? So we will use the method modulo usually die a large prime number, the number of mold can look at the data size of the problem is how much.

  Sometimes it happens in some cases, such as 3% 2% 2 = 1,5 = 1 (an analogy, generally modulus not so small), so the two numbers as a modulo number to deal with, which will be called hash collision , when do question to reduce such conflicts.

 

▎ example - a hash string [template]

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.

Input and output formats

Input formats:

 

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

Next N lines contains a string, a string provided.

 

Output formats:

 

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

 

Sample input and output

Input Sample # 1: 
5
abc
yyyy
abc
abcc
12345
Output Sample # 1: 
4

Explanation

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 .

   This question is completely template title, directly sets the idea just fine.

 

▎Code speaks louder than words!

  Man of few words said, directly on the code (see Notes)

  

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 string s;int n;int hash[10000],mod=19270817,k=30,ans=1;
 5 int Hash(string str)
 6 {
 7         int len=str.length();
 8         int value=0;
 9         for(int i=0;i<len;i++)
10         * = value + K value (( int ) STR [I] - 96 ); // switch hex 
. 11          return value; // here can actually look at the mold, but not so large-scale data 
12 is  }
 13 is  int main ()
 14  {
 15          CIN >> n-;
 16          for ( int I = . 1 ; I <= n-; I ++ )
 . 17          {
 18 is                  CIN >> S;
 . 19                  the hash [I] = the Hash (S); // after the storage of each string conversion the hash value 
20 is          }
 21 is          Sort (the hash + . 1, the hash + n-+ . 1 ); // sort, the purpose is to exclude the same hash value string 
22 is          for ( int I = 2 ; I <= n-; I ++ )
 23 is          IF ! (the hash [I] = the hash [I- . 1 ]) ANS ++; // If the hash values are different, then the string is not the same two 
24          COUT << ANS;
 25          return  0 ;
 26 is }

 

▎map is what?

  For this question is to say there is a big weapon --map. A brief introduction:

  1) header: #include <map>

  2) Definition: map <type, type> variable name;

       The first type is the type of the target array, a second variable is an array of values ​​of type

  3) use: map out the definition of what can be understood as any array subscript is an array, this is precisely the effect played that question just now beginning ideas

  4) For chestnut: For example, to define an array subscript is the integer array strings s, you can write map <string, int> s;

  How to do it just that question? Direct ordinary hash on it, do not write a comment.

 1 #include<iostream>
 2 #include<map>
 3 using namespace std;
 4 map<string,int>s;string str[100000];int n,ans;
 5 int main()
 6 {
 7     cin>>n;
 8     for(int i=1;i<=n;i++)
 9     {
10         cin>>str[i];
11         s[str[i]]=1;
12     }
13     for(int i=1;i<=n;i++)
14     {
15         if(s[str[i]]==1)
16         {
17             ans++;
18             s[str[i]]=0;
19         }
20     }
21     cout<<ans;
22     return 0;
23 }

 

Why ▎ stood map but not with the former method

  map looks nice, like arrays, in fact, we simply map the map, is simply violence query time complexity can imagine, this is very slow, and sometimes can be the subject of AC, but sometimes can not meet time requirements of the subject, it is honestly a hash of it under the strings.

 

Guess you like

Origin www.cnblogs.com/TFLS-gzr/p/10927097.html
Recommended