"Programming Pearl, character Pearl" 45678 Reading Notes -- programming skills

Source: Internet
Author: User
Preface

As mentioned in the previous article, "programming is always the future "! With a reliable problem analysis process and data structure selection, the "binary search" code can be correctly run before it appears, the main idea is implemented first on the draft, that is, pseudo code. However, due to the uncertainty of the pseudo-code execution results, a verification process is required. The author does not like this process very much, because it is very complicated and the conclusion is not necessarily correct (after all, there is no actual running result on the machine). In my opinion, give an algorithm question, know what algorithm is used, and the data structure. If it can be implemented using pseudo code, it is not far from the success.
However, I refuted my point of view (contradictions). The reason is: at least so far, I have written small programs and small algorithm questions. The verification process may have been implicitly solved.
Practice: optimal combination of Dynamic Planning Matrix concatenation

The trouble is that this evening we need to fill in a table to implement the "optimal combination of Dynamic Planning Matrix concatenation" algorithm. Through the decomposition of dynamic planning, the subscript of the table is messy, therefore, the process of filling out the table is boring (debugging has been performed many times ). I first solved this problem with pseudocode on the draft paper, but when I really typed the code, I found that the overall direction of "pseudocode" is beyond (the approximate structure ), many details have problems.

"Approximate" pseudocode:

For I = [0, n-1)
For J = [0, n-1-i)
Col =... // col is the column that fills in the table element
Min =...
For k = [0, I)
T = ....
If T <min
T = min
A [J] [col] = min;

The content in the ellipsis is incorrect after being typed in! You need to re-analyze the table filling process.

Messy analysis process

Sequential filling into n-1 groups, I = [0, N-2],

Each group has J = n-1-i elements that need to be filled in. So the first two lines of pseudo-code are like this.

For I = [0, n-1)
For J = [0, n-1-i)

First, the column value of the element to be filled in is col = I + J + 1. It is easy to find through observation. J is the row of the element to be filled in, therefore, (J, col) is the position of the element to be filled in.

While min computing is the bottleneck, drawing

It can be found that the (first element) Row and (second element) columns of the two auxiliary elements for Min calculation are not determined by the (J, col) that needs to be filled in at the moment. So:

Min = A [J] [J] + A [J + 1] [col] + TAB [J] * tab [J + 1] * tab [col];
The calculation of the next T is calculated by the above min:
T = A [J] [J + k + 1] + A [J + K + 2] [col] + TAB [J] * tab [J + K + 2] * tab [col + 1];
It is actually the same, but the red part adds k + 1.

The analysis process is not rigorous and delicate, but you have a clear idea.

View code void optimal_matrix (int * tab, int N)
{
Assert (n! = 0 );
Int ** A = new int * [N];
For (INT I = 0; I <n; I ++)
A [I] = new int [N],
: Memset (A [I], 0, sizeof (INT) * n );
Int I, j, T, Min, Col;
For (I = 0; I <n; I ++)
A [I] [I] = 0;
For (I = 0; I <n-1; I ++) // group counter
{
For (j = 0; j <n-1-i; j ++) // Number of counters per group
{
Col = I + J + 1;
Assert (COL + 1 <n + 1 );
Min = A [J] [J] + A [J + 1] [col] + TAB [J] * tab [J + 1] * tab [col + 1];
For (int K = 0; k <I; k ++)
{
Assert (J + K + 2 <n );
T = A [J] [J + k + 1] + A [J + K + 2] [col] + TAB [J] * tab [J + K + 2] * tab [col + 1];
If (T <min) min = T;
} //
A [J] [col] = min;
} //
} //
Cout <A [0] [n-1] <Endl;
Delete [];

It is not easy to implement the above rules and regulations. But if you develop the habit of "rules and regulations", even if there is no paper, you can only operate on mspaint. I believe that coding efficiency will be improved.

Assertion charm

"Scaffolding" is simply a "Verification Program" program, but I think "assertions" are more attractive. In every program, some variable data is crucial. Often, debug checks these variables to see if they are the same as the expected results. If they are different, my approach is: it is a headache to end debugging and start troubleshooting. Assert can clear a lot of error details, including Division 0, data beyond the specified range, array subscript out of the cloud. Therefore, adding assertions can logically ensure that your program will not go wrong, even if the reality is not the case.

Therefore, in the above "optimal combination of Dynamic Planning Matrix concatenation ",

Assert (n! = 0 );
Assert (COL + 1 <n + 1 );
Assert (J + K + 2 <n );

To ensure that the array subscript is out of bounds. Obviously, if the subscript is out of bounds, it will be a devastating bug.

In addition, a show function is added:

Void show (INT ** A, int N)
{
For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <n; j ++)
Cout <SETW (6) <A [I] [J];
Cout <Endl;
} //
Cout <Endl;
}

This is part of the verification program, in addition to an article on the program running time chain, the method inside is good, not only can be accurate to Ms, but also can be us, http://www.cnblogs.com/ma6174/archive/2012/01/03/2310996.html

One algorithm question

Unfortunately, Chapter 1 does not understand much. It is probably because readers need to understand how to predict program overhead !! On the contrary, Chapter 8 is much more interesting. In chapter 8, we propose to find the largest and continuous subsequence in a numerical sequence, and specify the sum of all negative subsequences as 0.

"If you have not read the "programming Pearl", jump to the fourth point ."

  1. The time overhead of the most primitive exhaustive algorithm is greater than O (N ^ 3 );
  2. Another exhaustive algorithm is the square algorithm. By saving intermediate results or pre-processing data, the repetitive computation is saved. It is a memorandum algorithm (Simple Dynamic Planning), and the overhead is reduced by an order of magnitude O (N ^ 2 );
  3. Division and Control Law, This is not expected, the overhead again drops O (nlogn );
  4. I have done similar questions before, so the first thought of this method is. The image point is "Eat and pull"-scanning algorithm, and the running time is O (n ).
For I = [0, n)
T + = A [I]
If T <Max case
Max = T
If T <0 case
T = 0
End.

 

ACM preliminary round of the title is this "Eat and pull", but and here a little different, in fact is greedy algorithm: http://acm.hdu.edu.cn/diy/contest_showproblem.php? PID = 1005 & cid = 14855 & hide = 0

 

Exercise 10th questions after class, "Find The subsequence whose sum is closest to 0 or the subsequence closest to a certain number", and try to solve the problem by using the "Eat and pull" algorithm above, but it fails. You can only follow the square algorithm mentioned above. The pseudo code is as follows:

Nearest = inf
For I = [0, n)
For J = [I, n)
T = tab [J]-tab [I]
If nearest> | T | case
Nearest = T
Index the end array with a negative number

In chapter 8, the most interesting thing is that "the array index subscript can appear in the plural "! The program that has been written for nearly two years does not even know this thing, and it is slightly self-explanatory.
If the original array a [n], Pa = & A [1], then pa [-1] is also a [0]. However, how can we use such a clever thing? Everyone has written "Bubble Sorting" in the beginning to see what effect can be achieved by using this "clever? Pseudocode: Bubble (A, n)
Mustbe (n> 1)
Pa = & A [1]
For I = [0, n)
For J = [0, n-I)
If a [J]> pa [J] Case
Swap (A [J], pa [J])
End

Yes, it does not improve the running overhead and efficiency of bubbling, but the code is much more elegant: by positioning Pa on the second element of, therefore, a [J] and pa [J] are not the same element. Pa [J] is behind a [J], even if their subscript is the same. The author suddenly came up with a realistic and useful example. When we first learned programming, we almost encountered such a problem. Given a number array, we would calculate the sum of the elements of the array, original ideas:

Sum (A, n)
For I = [0, n)
Sum + = A [I];
End

Look at the above "clever" pseudocode:

Sum (A, n)
Pa = & A [n-1]
T = n> 1;
For I = [0, T)
Sum + = (a [I] + pa [I | 0x8000]) // The subscript becomes negative.
If N & 1 case
Sum + = A [T]
End

Yes, he did the addition n times, but the addition was not reduced, but there was also a small optimization: The I judgment and auto-increment in the for loop were halved! The overhead of addition and subtraction is greater than that of bitwise operations."Aha, what a brainwave !".A very young man in literature and art is very poetic...

End

At the beginning of chapter 8, "Shit, fuck, and Cao" sighed a little more. (You can see why I have such sighs when I look at "The Journal account for reading books ), this is what I want to read. The next note will write more "code optimization", because there are also many related topics in "deep understanding of computer systems!


After Friday, limit l 06,201 2

Spoof http://daoluanxiaozi.cnblogs.com/
 

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.