Summary of Programming

Source: Internet
Author: User
Column 1:
Bitvector operation:
#define BITSPERWORD 32#define SHIFT 5#define MASK 0x1F#define N 10000000int a[1 + N/BITSPERWORD];
void set(int i) {        a[i>>SHIFT] |=  (1<<(i & MASK)); }void clr(int i) {        a[i>>SHIFT] &= ~(1<<(i & MASK)); }int  test(int i){ return a[i>>SHIFT] &   (1<<(i & MASK)); }    (a[i>>SHIFT] &   (1<<(i & MASK))) != 0

P4. how to generate K numbers that are within range [1-N) and no two numbers are equal and their position is random.
My idea: allocate a array with n bit to record the generated number if the number is generated then regenerate until the number is not generated. O (n) space. Too big.

Still use random Algorithm
For (INT I = 0; I <n; ++ I)
{
A [I] = I;
}

For (INT I = 0; I <K; ++ I)
{
Swap (A [I], random (I, n-1 ));
}

Loop Invariant:
The probability of the permutation A [0] ~ A [I-1] is (n-I )! /N !.
A [0] ~ A [k] Saved The K unique random generated numbers within range [0-n-1].

P5. sort n numbers which cannot be stored in the memory. (bucket sort .)
1. Divide the numbers into k parts and each part can be stored in the memory.
2. use K file on the disk to store the temporary sort result. the first file stores the number 0-n/K, the second file stores the number N/k + 1-N/K * 2 and so on.

3. Traverse each part of numbers and store the numbers into corresponding file according to the value of number.Bucket sort
4. After this step sort the each file and merge them together then it is done.
Sort using counting sort.

O (kN) time and O (N/K) memory space, O (n) disk space.

P9. how to ensure data is not a random value without initialization.

Column 2:
1. The new word formed by changing the positions of each letter of a word is the variant word of the original word. Such words can be sorted by signature, that is, their signatures are consistent. Signatures such as CABD, ACDB, and bcad are all ABCD (sorted by letter ). In this way, all such words have the same signature and can be sorted and unified.

2. The equivalence class can be signed to represent all element features in the equivalence class. The principle is similar to the hash operation (sorting letters is a hash function ).

3. Binary Search not only finds an element in the sorting set (the query rule is equal), but also determines that a number does not exist (that is, it is not equal ).
Binary Search is used to quickly narrow down the search scope of an algorithm. Therefore, binary search can be used with linear search.
For example, find a number that does not exist in a range. For example, if there is a set of numbers, the range of each number is [1-100], and find a number within [1-100, but does not belong to the set. Divide the set into two parts. The range of the first part is [1-50], and the second part is [51-100].
If the number in a set is less than 50, it indicates that the number in the range is smaller, and the search continues in the range.

4. Rotate algorithm, one is continuous replacement of O (N) (this method is clever), the other is transpose, O (2n ).
In P5., it is required to convert ABC to CBA. The Code is as follows:
/*
* Reverse string a from left to right
* L = left site index
* R = right site index
*/
Void reverse_r (char * a, int L, int R)
{
While (L <r ){
Swap (A [L ++], a [R --]);
}
}
/*
* Change AB to Ba
* N = length of whole string "AB"
* Bit = length of
**/
Template <typename T>
Void rotate_rcur (T * a, int N, int bit)
{
Reverse_r (A, 0, bit-1 );
Reverse_r (A, bit, n-1 );
Reverse_r (A, 0, n-1 );
}

// Change ABC to CBA
// Bit1 = length of
// Bit2 = length of B
// N = lenght of whole string "ABC"
Template <typename T>
Void multirotate (T * a, int N, int bit1, int bit2 ){
Rotate_rcur (A, bit1 + bit2, bit1 );
Rotate_rcur (a + bit2, n-bit2, bit1 );
Rotate_rcur (A, n-bit1, bit2 );
}

Column 3:
The data structure is used as a table, and the data is recorded in an array. The input is used as a subscript to directly access the array, without the search by conditions such as if, else, or switch.

Column 4: binary classification
If we see the order, we can think of the binary method.
P7: a line segment problem. Use binary search. Determine whether the input is on the online segment or under the line segment ).
P9: returns x ^ N using a binary method. If n is an even number, you only need to calculate x ^ (n/2, do not calculate twice x ^ (n/2 ). If n = an odd number, x ^ (n) = x * x ^ (n-1). Therefore, O (logn) is called.
Double exp (Double X, int N)
{
If (n = 0) return 1;
If (even (N )){
Return square (exp (x, n/2 ));
}
Else {
Return x * exp (x, n-1 );
}
}

Chapter1:
Some basic things:
1. Use space for time change, such as bucket sorting and table creation,
2. narrow down the scope of calculation, and divide the formula into two parts, such as range search and the N power of X.
3. The circular invariant is very important. It is necessary to clarify the meanings of different variables before, during, and after the loop. For example, the Left shift of the string.

Column 8:
Algorithm Analysis and Design:
1. Understand the problem. For example, the maximum and continuous strings cannot be separated. The array contains negative values. When all values are negative and 0.
2. Violence Law. Generally, the complexity is either the square or the power of three, or the space or time must be used.
3. Start optimization. The key to optimization is to find the place where repeated computing is located. For example, 8.1 of the algorithm is O (n ^ 3) complexity, but the analysis finds that for a given I, it is not necessary for every J to be calculated from I to J, cumulative sum can be used to reduce repeated computations. The complexity is reduced to O (N ^ 2 ). A typical space-to-time approach allows new computing to be performed based on the computed results.
4. Based on the brute-force method, check whether the time complexity can be improved by sharding, sorting, and binary.
8.3 I used the bipartite method to solve this problem. I divided the array into two halves. I calculated the largest sum on the left, the largest sum on the right, and the largest sum in the middle, the maximum complexity of the three values is O (nlogn ).
5. Of course, you can use greedy, dynamic planning, line segment tree, and fast partitioning methods at the end. Let's try these awesome stuff. Dynamic Planning is still quite common.
Dynamic Planning is a typical method for changing the space time. Split the subproblem, calculate the subproblem, save the subproblem solution, and then use the subproblem solution to calculate the original problem. So as to reduce the time complexity, of course, the space complexity increases.
8.4 The dynamic planning method is used. In fact, the idea of dynamic planning is consistent with that of the cycle without changing. If the sum of DP [I] is already used, X [0... i], DP [I + 1] indicates X [0... I + 1.
For this question, you must note that you cannot just save the maximum string sum of DP [I] That represents X [0... I. If the optimal solution of DP [I] is not necessarily connected to X [I + 1], you must save the maximum string ending with X [I] And Ed [I], therefore, when calculating DP [I + 1], you can obtain ed [I + 1] = max (Ed [I] + X [I + 1], 0 ), DP [I + 1] = max (DP [I], Ed [I + 1]).

The key to dynamic planning is to find Sub-problems. If you cannot find a sub-problem, it will be difficult to solve it.

P10: calculate the maximum string sum closest to 0. It seems that no dynamic regulation is available, because there is no optimal sub-structure, such as-5, 3,-3, and 5 are all added to 0, but the last 5 is removed, and the three first sides are added to-5, which is not the optimal sub-solution, the optimal sub-solution is 3 +-3 = 0, so no dynamic gauge is used.
Method: use cumulative array, cum [I] = cum [I-1] + X [I]. If cum [I] = cum [J] exists, sum (X [I... J]) = 0. So sort the cum and find the smallest difference between the two. Of course, save the subscript corresponding to the cum. Otherwise, the sorting will be out of order.

P11: cumulative Array

P12: This question is clever, X [0 .. n-1] All Initialization is 0, N operations, for I = [L, u] X [I] + = V, so if you add one operation, it takes O (n) time, O (N ^ 2) for N operations ). The cumulative array is used in the book.
For I = [L, u], you only need to record cum [L-1] =-V, cum [u] = v. In the final sum, for (INT I = n-1; I> = 1; I --) x [I] = x [I + 1] + cum [I ],

P13: N * n matrix. Find the maximum submatrix and maximum. O (N ^ 3 ),
For (INT I = 0; I <n; I ++) // n
{
For (int K = 0; k <n; ++ K) sum [k] = 0;
// N
For (Int J = I; j <n; ++ J ){
// N
For (int K = 0; k <n; ++ K ){
// N
Sum [k] + = A [J] [k];
}
T_max = maxsum (sum, k );
If (t_max> MAX) max = t_max;
}
}

This section focuses on the importance of the cumulative array, that is, space for time.

Column 9:
This section describes how to obtain the first position of an element when binary search processes the same element. The key is that when an element equal to T is encountered, do not exit the search, but think that there may be T on the left of the element, so continue to search on the left (including a [m]).
Pseudo code:
L =-1, u = N;
While l + 1! = U
// A [l] <t, a [u]> = T, L + 1 <u
M = (L + N)/2
If (A [m] <t) L = m;
Else u = m
P = u
If P = n | A [p]! = T
Return-1;
Return P;

P6: How can I determine whether a character is digit, letter, upper, or lower? Use the tabulation method.
 

Column 10:
This chapter mainly describes sorting (insert sorting and quick sorting). The key is that when the array elements are very small, they do not need to be sorted quickly, but return directly, and then insert the sorting, it takes only O (n) Time to sort an almost ordered array by inserting a sort pair.
1. When the number of array elements is smaller than a certain threshold value, insert sorting can be directly used.
Void qsort (int * a, int L, int R)
{
While (L <R)
{
If (R-l <32) // prevents deterioration of the split
{
Insertsort (a + L, R-l + 1); // insert sorting
Return;
}
Int I = partition (A, L, R );
Qsort (A, L, I-1 );
L = I + 1;
}
}

2. You can use loops to remove recursion.
Void qsort (int * a, int L, int R)
{
While (L <r) // prevents excessive Recursion
{
Int I = partition (A, L, R );
Qsort (A, L, I-1 );
L = I + 1;
}
}

P4: the worst of the recursive stack in the quick row is O (n). If we modify it to a partition, we will first perform partition on the smaller half, the larger half uses loop to eliminate recursion, which can be reduced to a stack of the O (logn) size. See "Introduction to algorithm" 7.4
P5: sorts binary variable-length strings. Similarly, by sorting, we first move all the values with a length of 1 to the leftmost, and then recursively analyze the values with a length greater than 1. Strings with a length greater than 1 are compared based on the first character. 0 is at the beginning, 1 is at the end, and the code is as follows:
Void m_sort (int l, int U, int Len ){
If (L> = u) return;
Int I = L-1, j = l, m = 0;
While (j <= u ){
If (strlen (STRs [J]) <Len ){
Char * t = STRs [I + 1];
STRs [++ I] = STRs [J];
STRs [J] = T;
}
++ J;
}

M = I;
J = m + 1;
While (j <= u ){
If (STRs [J] [len-1] = '0 '){
Char * t = STRs [I + 1];
STRs [++ I] = STRs [J];
STRs [J] = T;
}
J ++;
}

M_sort (m + 1, I, Len + 1 );
M_sort (I + 1, U, Len + 1 );
}

P11: For the same fast partitioning question, divide the array into three blocks. <t = T> T, divide the array into two blocks. <t> = t, then, divide the> = t part into two parts: = T and> T.
Code:
/*
* FAT partition, there are several T in the array
*
* Partition the array into three parts: <t = T> T
*
**/
Void fat_partition (int * a, int L, int R ){
Int I = L;
Int J = L + 1;
Int T = A [l];
While (j <= r ){
If (A [J] <t ){
Int TMP = A [I + 1];
A [I + 1] = A [J];
A [J] = TMP;
++ I;
}
++ J;
}
A [l] = A [I];
A [I] = T;

J = I + 1;
While (j <= r ){
If (A [J] = T ){
Int TMP = A [I + 1];
A [I + 1] = A [J];
A [J] = TMP;
++ I;
}
++ J;
}
}

The key to this section is to learn to use the partition method of the Quick Sort To quickly divide a set into two parts, and then use the divide and conquer method to process each part, or only process one part. For example, the Select algorithm is typically used to process only a portion of data.

Column 12:
This section describes how to randomly retrieve M numbers from N numbers.
1. Using the probability method, the number of M numbers is randomly obtained from N numbers, and the probability of each number is M/N, which is illustrated in the book and can be proved.
Code: Select = m
For I = [0, n)
If (bigrand () % (n-1) <select)

Select --;

Print I

2. Use the set-based algorithm to save random numbers with a set. The next random number will be compared with the element in the set. If yes, the random number will continue. Otherwise, insert into the set.
3. Shuffle algorithm, with the first m values.

This part can be viewed in combination with Chapter 1 of Introduction to algorithms.

P2: a random sequence can be generated if the probability of each element is the same.
P9: this is an awesome algorithm. The set-based algorithm ensures that M random numbers can be obtained after m times.
P10: the number of M is required for a large stream. The answer is weird. The online statement is: each time a record is input, a random number ranging from 0 to 1 is randomly generated. Use these random numbers to maintain a heap with a size of M. It is equivalent to finding the maximum M (minimum) from N ).


Column 13:
This section describes the features of several different data structures, including arrays, linked lists, BST, bins, and bitvector.
Multiplication and division can be replaced by shift.

Column 14: This section mainly introduces heap, which is often used when there are many priority queues but the memory does not store all the data. It can maintain the maximum (small) heap of N, evaluate the number of N small (large) values.

 
Column 15: string processing. suffix table is learned. A suffix table is a pointer array char * A [n]. A [I] points to & C [I] (C is the original array ), in this way, a [n] contains all the suffixes of C. Sort a as long as the adjacent A is compared. O (nlogn), For details, see column 15.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.