[QQ group: 189191838, interested in algorithms and C ++ can come in]
The first problem is to find the maximum number of 10 million data records.
The first response to this problem is that there are many sorting algorithms. Among them, the most frequently used is in quick scheduling. You only need to get the first 1 million for multiple parations.
Another idea is to build a heap with a size of 1 million and then continually adjust the incoming data. When the data found is smaller, the advantage will be particularly obvious. For example, if 1 million is changed to 100, heap is the best choice.
The second problem is to sort 10 million data entries with a maximum of 1 million entries without duplicates.
In fact, this can also be sorted by heap and processed quickly. However, bitmap can also be used for processing. The time complexity of bitmap is N, which is faster than that of Bitmap. It only requires 1 million/8 bytes.
If you change the question to 999999 (1 million-1), the data is composed of 1-million records, and there are no duplicates. Then, you can find the missing data. The advantages of Bitmap will be enhanced. It is much better than other sorting methods.
The third problem is that 1 million + 1 pieces of data are repeated in pairs, and only one piece of data is single.
Of course, we can use sorting. However, using all the data and & will be much faster. Therefore, bit operations are sometimes important.
The fourth question is whether a word exists in a book.
This problem can be found in many ways, such as sorting, through the base sorting method, and then binary search. This is good, but the efficiency is not very high.
Or use hash to hash and compare all words in the book.
I think the best way is to build a dictionary tree tire, which is very fast.
Fifth, the number of M in 1--n is randomly generated.
If the number of duplicates can be generated, it is very easy to directly rand (n) m times. But what if it cannot be repeated?
1. Use the random idea to open an N-large array, and then for I = 1... m; a random number k is generated each time. If K> I is used, the number in the array is exchanged. Otherwise, the random number is not exchanged and M is repeated.
2. Using the concept of probability, the probability of the first number in the M number is M/N, then a number k is randomly generated (1... n) if K is smaller than m, it is added to m, and m = S-1 is used to process the second number 2. At this time, the probability of adding 2 to the array should be (m) /N-1 .... likewise
I didn't want to paste the code, but stick it again."
1 public static void getm (int n, int m) {// obtain the equi probability where the number of M is 2 for (INT I = 0; I <n; I ++) {3 random = new random (); 4 int K = random. nextint (10000); 5 6 if (M> 0 & K % (n-I) <m) {7 system. out. print (":" + I + ""); 8 m --; 9 If (M = 0) {10 break; 11} 12} 13} 14}View code
The sixth question is, how can we get the same substring for two strings? (Not a subsequence) subsequences are not consecutive, but must be consecutive.
The first reaction is to solve the problem with the idea of dynamic planning. (The time complexity of the violence law is too high)
Second, you can create a suffix array, suffix array, and sort. Compare to get the answer. Complexity is better than dynamic planning. It's just a space change. The code will not be pasted.
The seventh question is a bunch of word sets m, a string S. Find the number of words that s can find in M. For example, M = {good, thank, you, are, do}, S = "goodoayouk". Three words can be found: Good, do, and you.
Attackers can exploit this vulnerability to make it hard-matched;
A better method should be to search by using the AC automatic machine. This is good, fast!
(This article is only used as my own summary, so I can search other articles for relevant details, and typographical layout is not my long term, so we will do it .)
Not complete...
All rights reserved. You are welcome to reprint it. However, please indicate the source: Copyright 1.
A brief summary of several algorithms