1. To give a word a, if another word B can be obtained by exchanging the letter order in the word, B is the brother word of A, for example, the word army and Mary are brother words.
Now we need to provide a solution to find out which brothers of words are entered Based on the given dictionary for words entered by the user. Please describe the data structure and Query Process in detail, and the time and space efficiency should be as high as possible.
Typical application of the dictionary tree. Generally, the structure of the dictionary tree is organized by the 26th tree. Each node corresponds to a letter, it is the matching of a letter and a letter. the time complexity of the algorithm is the length of the word N, which is very efficient. Therefore, this topic can define a dictionary tree as a data structure to query, and the time efficiency will be very high. In this way, it is converted into searching for sibling words in a dictionary tree, as long as you store a vector-structured container in the prefix of the dictionary tree, it is time complexity at the constant level, and the efficiency is very high ..
The data structure can be defined as follows:
[CPP]View plaincopy
- Struct word
- {
- Vector <string> brother; // The sibling word used to save each word.
- Word * Next [26]; // each node in the dictionary tree represents one character and points to the next character
- };
As shown in the above data structure, the establishment of the dictionary tree is completed in the pre-processing phase. First, the dictionary tree is created based on the words in the dictionary. When the dictionary is created, it needs to be processed slightly, that is, for example, the interaction between pots, stop, and tops.
If it is a sibling word, you should first encounter a pots word in the alphabetical order. Then I first sort it and the result is opts, then four nodes are created in the dictionary tree.
It is o-> P-> T-> S. Of course, this is different layers. Add the word pots to the vector container brother at node S. When the stop operation occurs
In the same way, the sorting is opts. If the four nodes have been created, you only need to add the word stop to the vector container brother at the fourth node S, tops word processing method is the same.
In this way, after the dictionary tree is created, the efficiency of querying sibling words will be high, which is more efficient than hash. When you find the word of a sibling in tops, sort it first, that is, opts, search for opts in the dictionary tree, and output the words in the vector container brother at S to all the brothers in tops.
2. Several data items are maintained in the system. We can classify the data items into three levels.
Data items are classified into A, B, and C ...... several categories. The categories produced by each level-1 classification method can be classified into A, B, and C by level-2 classification method ...... several subcategories. Similarly,
The category generated by the second-level classification method can be classified into I, II, III according to the third-level classification method ...... data items in each category are numbered from 1. Me
You need to output logs for each data item in the form of a key_value pair. When writing logs, the user provides the three-level category name, data item number, and log key, a total of five key values, for example
For example, write_log (a, a, I, 1, key1). When obtaining logs, the user provides the three-level category name and data item number. A total of four key values are returned.
Key_value pair, such as get_log (a, a, I, 1, key1 ),
Describe a Data Structure to store these logs and calculate the time complexity of writing logs and reading logs.
3. How to dynamically allocate and release memory in C and C ++? What are their differences?
The difference between malloc/free and new/delete, please refer to here http://blog.csdn.net/hackbuteer1/article/details/6789164
4. array al [0, mid-1] and Al [mid, num-1] are their respective orders. Merge is performed on the two sub-ordered segments of array al [0, num-1, obtain the overall order of Al [0, num-1. The space complexity is O (1 ). Note: The al [I] element supports the '<' operator.
[CPP]View plaincopy
- /*
- Arrays A [begin, mid] And a [Mid + 1, end] are ordered separately. Perform merge on the two child segments to obtain the ordered array of a [begin, end. Requires that the space complexity be O (1)
- Solution:
- 1. Two ordered segments A and B. A is in front of B and B is immediately behind a. Find the first number of a greater than B [0] a [I], A [0... i-1] is equivalent to the valid segment after merge, in B find the first number greater than a [I] B [J],
- Loop right to the array a [I... J-1] and move the J-K bit so that the front part of the array a [I... J-1] is ordered
- 2. So loop merge
- 3. The right shift of a loop is performed by first reversing the sub-segment and then reversing the whole. The complexity is O (l), and L is the length of the sub-segment that needs to be moved cyclically.
- */
- # Include <iostream>
- Using namespace STD;
- Void reverse (int * a, int begin, int end) // reverse
- {
- For (; begin <end; begin ++, end --)
- Swap (A [begin], a [end]);
- }
- Void rotateright (int * a, int begin, int end, int K) // shifts right of the loop
- {
- Int Len = end-begin + 1; // array Length
- K % = Len;
- Reverse (A, begin, end-k );
- Reverse (A, end-k + 1, end );
- Reverse (A, begin, end );
- }
- // Merge and sort the ordered arrays A [begin... mid] And a [Mid + 1... end ].
- Void Merge (int * a, int begin, int end)
- {
- Int I, J, K;
- I = begin;
- J = 1 + (begin + end)> 1); // the priority of bit operations is relatively low. You need to add a bracket to the outside and forget to add the brackets at the beginning, cause errors many times
- While (I <= end & J <= end & I <j)
- {
- While (I <= end & A [I] <A [J])
- I ++;
- K = J; // temporarily Save the position of pointer J
- While (j <= end & A [J] <A [I])
- J ++;
- If (j> K)
- Rotateright (A, I, J-1, J-k); // array a [I... J-1] loops right shift J-K times
- I + = (J-k + 1); // The first pointer moves backward, because after the right shift of the loop, the array a [I .... I + J-K] is ordered
- }
- }
- Void mergesort (int * a, int begin, int end)
- {
- If (begin = end)
- Return;
- Int mid = (begin + end)> 1;
- Mergesort (A, begin, mid); // recursively merges a [begin... mid] into an ordered array
- Mergesort (A, Mid + 1, end); // recursively merges a [Mid + 1... end] into an ordered array
- Merge (A, begin, end); // sort the sorted arrays A [begin... mid] And a [Mid + 1... end ].
- }
- Int main (void)
- {
- Int N, I, a [20];
- While (CIN> N)
- {
- For (I = 0; I <n; ++ I)
- Cin> A [I];
- Mergesort (A, 0, n-1 );
- For (I = 0; I <n; ++ I)
- Cout <A [I] <"";
- Cout <Endl;
- }
- Return 0;
- }
5. What are the differences between threads and processes? How can we understand the "thread security" issue?
Answer: both processes and threads are the basic units for running programs that the operating system understands. The system uses this basic unit to realize the system's concurrency for applications.
1. In short, a program has at least one process and at least one thread.
2. The thread division scale is smaller than the process, making the multi-threaded program highly concurrent.
3. In addition, the process has independent memory units during execution, and multiple threads share the memory, greatly improving the program running efficiency.
4. There is a difference between a thread and a process during execution. Each Independent thread has a program running entry, sequence execution sequence, and program exit. But the thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control.
5. From a logic point of view, multithreading means that multiple execution parts in an application can be executed simultaneously. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation. This is an important difference between processes and threads.
Algorithm and Program Design 1,
When a web crawler crawls a webpage, it crawls all the URLs on the website starting from the specified URL site portal.
Link. After capturing the page corresponding to the next link, the link on the page is also crawled to complete the in-depth traversal. To simplify the problem, we assume that each page has at most one link,
For example, link from www.baidu.com/a.htmlto
Bytes
Www.baidu.com/ B .html, you can also get a webpage without any link. When the same URL or the ultimate page without any link is captured
. After crawlers crawl these pages, they create a one-way linked list to record the captured pages,
For example, a.html> B .html> x.html...> null.
Q: When crawlers start from www.baidu.com/x1.htmland www.baidu.com/x2.htmland start with two single-direction table
After a one-way linked list, how do I determine if they have captured the same URL? (Assume that the page url is tens of billions and the storage resources are limited. The hash method cannot be used to determine whether the same URL is included)
Describe the corresponding algorithm before providing the corresponding code implementation. (Only the judgment method code is provided, and no crawler code is required)
Intersection of two one-way linked lists.
Algorithm and program design 2,
4. There is a structure as shown in. It consists of layer nesting. A parent layer can only contain layers that are parallel in the vertical or horizontal direction. For example, layer 1 can contain three vertical directions: 2, 3, and 4.
Layer, Layer 2 can contain layers 5 and 6 in the horizontal direction, and the empty layer can contain data nodes. The so-called empty layer is a layer that does not contain child layers, each blank layer can contain several data nodes, or each layer can contain no packages.
Include.
In this structure, we draw a line vertically. We agree that we can only go through one data node in each sub-layer. In this case, each line can go through multiple data nodes, alternatively, line 1 can pass through 3, 5, and 8 data nodes without passing through any data node. Line 2 only goes through 14 data nodes.
(1) A function is provided to determine whether two data nodes can be drawn at the same time and provide specific code.
(2) A function is provided to output a sequence of data nodes in which all lines can be drawn, and pseudo code implementation can be provided.
Idea: (1) Determine whether the next rectangular frame of the same rectangle of the two numbers belongs to the horizontal or vertical arrangement, horizontal sorting is not allowed.
(2) Use the backtracking algorithm to find all the strings in a straight line, and use the two strings to determine whether they are in the same line for pruning.
System Design Questions
1. I believe everyone has used the suggestion function in the baidu search box. How can I implement the suggestion prompt function in the baidu search box? Provide Implementation ideas and main data structures and algorithms. Are there any optimization ideas that can make time and space the most efficient?
Use the dictionary tree to calculate the prefix and top K for statistical sorting of Hot Words
2. For files a and B of GB size, the content in the AB file is an unordered row and a positive integer (no more than 2 ^ 63). Please design a solution, output the numbers in both files. Use a machine with no more than 16 GB memory and sufficient disk space.
The solution specifies the key tool classes used for Java programming, and why?