The beauty of programming Reading Notes 2.15, the beauty of Reading Notes 2.15

Source: Internet
Author: User

The beauty of programming Reading Notes 2.15, the beauty of Reading Notes 2.15

Http://blog.csdn.net/pipisorry/article/details/39083073

Problem:

Returns the maximum value of the Child matrix of a two-dimensional array (matrix.

Also visible: http://poj.org/problem? Id = 1050



Solution 1: (for explanations, see the notes)

Each child matrix is determined by the length of the column, the row length, and the element position in the upper left corner. If we specify the element position (I, j) and Column Length (c) in the upper left corner, we can find all these submatrices and the largest ones. Then, if the column length is changed to c, we can calculate the largest and submatrices in the upper left corner with (I, j. Find the largest and sub-matrix at the top left corner to solve the problem. So that OPT (I, j, c) indicates taking (I, j) as the upper left corner, the column length is the sum of the largest and submatrices of c, and OPT (I, j) indicates taking (I, j, j) is the optimal solution in the upper left corner, while S (I, u, v) indicates the sum of all elements from column u to Column v in row I. Then

OPT (I, j, c) = OPT (I + 1, j, c) + S (I, j, j + c-1)

OPT (I, j) = max {OPT (I, j, c): 1 <= c <= n}

Where j + C-1 <= n. When I> m, OPT (I, j, c) = 0. There are a total of O (mn) OPT (I, j) subproblems, and each OPT (I, j) can have n decisions. Therefore, the total solution scale is O (Mn) OPT (I, j, c ). Every such subproblem can be solved within O (1) Time (think about how to do it). Therefore, the time complexity is O (Mn ).


/* Direct enumeration O (N ^ 2 * M ^ 2 * O (sum () Time Limit Exceeded */static int submatrixSum (int ** a, int row, int row_end, int col, int col_end) {int sum = 0; for (int I = row; I <= row_end; I ++) for (int j = col; j <= col_end; j ++) sum + = a [I] [j]; return sum;} static int maxSubmatrixSum1 (int ** a, int n) {int max_sum = INT_MIN; int sum; int max_row, max_row_end, max_col, max_col_end; for (int row = 0; row <n; row ++) {for (int col = 0; col <n; col ++) {// position in the upper left corner of the Child matrix for (int row_end = row; row_end <n; row_end ++) for (int col_end = col; col_end <n; col_end ++) {// determine a sub-matrix sum = submatrixSum (a, row, row_end, col, col_end ); // calculate the sum of the Child matrices and if (sum> max_sum) {max_sum = sum;/* max_row = row; max_row_end = row_end; max_col = col; max_col_end = col_end; * // printf ("% d \ n", max_sum );}}}} /* printf ("\ n col \ tcol_end \ n % d \ t % d \ n", max_col, max_col_end); // position of the output sub-matrix (four attributes) printf ("row % d \ nrow_end % d \ n", max_row, max_row_end); */return max_sum ;}

Solution 2:

/* DP algorithm O (N ^ 2 * M) */static int maxSubmatrixSum2 (int ** a, int n) {int *** row_sum = (int ***) malloc (sizeof (int **) * n); for (int I = 0; I <n; I ++) assert (row_sum [I] = (int **) malloc (sizeof (int *) * n); for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) assert (row_sum [I] [j] = (int *) malloc (sizeof (int) * n )); // calculate row_sum [I] [j] [c] for and (int I = 0; I <n; I ++) // initialize row_sum [I] [j] [c] To 0for (int j = 0; J <n; j ++) memset (row_sum [I] [j], 0, sizeof (int) * n );//!!! For (int I = n-1; I> = 0; I --) {for (int j = 0; j <n; j ++) {for (int c = j; c <n; c ++) if (c = 0) row_sum [I] [j] [c] = a [I] [c]; elserow_sum [I] [j] [c] = row_sum [I] [j] [c-1] + a [I] [c];} // convert row_sum [I] [j] [c] to the sum Optimal Solution for (int I = n-2; i> = 0; I --) {// row_sum [n-1] [j] [c] unchanged for (int j = 0; j <n; j ++) for (int c = j; c <n; c ++) {if (row_sum [I + 1] [j] [c]> 0) row_sum [I] [j] [c] + = row_sum [I + 1] [j] [c] ;}// calculate [I, j] is the optimal rectangle int ** optij = (int **) malloc (sizeof (int *) * n); for (int I = 0; I <n; I ++) optij [I] = (int *) malloc (sizeof (int) * n); for (int I = 0; I <n; I ++) memset (optij [I], INT_MIN, sizeof (int) * n); for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) for (int c = j; c <n; c ++) {if (row_sum [I] [j] [c]> optij [j] [j]) optij [j] [j] = row_sum [I] [j] [c] ;}// obtain the overall optimal solution int max_sum = INT_MIN; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (optij [I] [j]> max_sum) max_sum = optij [I] [j] ;}return max_sum ;}

Solution 3:


/* Optimal algorithm AC 63 MS */static int maxSubmatrixSum (int ** a, int n) {// initialize col_sum, col_sum [I] [j] [k] is the sum of the elements in column k between row I and row j and int *** col_sum; assert (col_sum = (int ***) malloc (sizeof (int **) * n); for (int I = 0; I <n; I ++) assert (col_sum [I] = (int **) malloc (sizeof (int *) * n); for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {assert (col_sum [I] [j] = (int *) malloc (sizeof (int) * n); memset (col_sum [I] [j], 0, sizeof (Int) * n) ;}// calculate the sum of column k between row 0th and row j (> = 1) and for (int k = 0; k <n; k ++) {col_sum [0] [0] [k] = a [0] [k]; // initialize col_sum [0] [0] [k] for (int j = 1; j <n; j ++) col_sum [0] [j] [k] = col_sum [0] [j-1] [k] + a [j] [k]; //!} // Calculate the sum of column k between row I and row j (int k = 0; k <n; k ++) {for (int I = 1; I <n; I ++) for (int j = I; j <n; j ++) col_sum [I] [j] [k] = col_sum [I-1] [j] [k]-a [I-1] [k];} // calculate the maximum submatrix and int max_mat_sum = INT_MIN; for (int I = 0; I <n; I ++) {for (int j = I; j <n; j ++) {int row_sum = 0; // The largest row array and (compression matrix) for (int k = 0; k <n; k ++) {row_sum + = col_sum [I] [j] [k]; if (row_sum <0) row_sum = 0; if (row_sum> max_mat_sum) max_mat_sum = row_sum; }}return max_mat_sum ;}



Test:

Int main () {assert (freopen ("BOP \ maxSubmatrixSum. in "," r ", stdin); // int cases; // Number of test cases // scanf (" % d ", & cases ); // while (cases --) {int n; // in each case, the matrix dimension scanf ("% d", & n); int ** a = (int **) malloc (sizeof (int *) * n); for (int I = 0; I <n; I ++) a [I] = (int *) malloc (sizeof (int) * n); for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) scanf ("% d", & a [I] [j]); // printf ("% d \ n", maxSubmatrixSum1 (a, n )); // printf ("% d \ n", maxSubmatrixSum2 (a, n); printf ("% d \ n", maxSubmatrixSum (a, n )); //} fclose (stdin); return 0 ;}
Test Case:

3


2
1 1
1 1


4
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2


3
3-1 4
3-1 4
3-1 4




Output:
4
15
18

From:

Http://blog.csdn.net/pipisorry/article/details/39083073

Ref:

Maximum Submatrix & Largest Rectangle

Beauty of programming p190



10 Reading Notes for insects (including content excerpt, feelings after reading, and accumulation of good sentences)

Childhood Reading Notes
1. Everyone experienced it in childhood. Childhood is wonderful, childhood is happy, childhood is happy, childhood is worth remembering ...... Gorky's childhood was so horrible and miserable that it was hard to recall.
Poor Gorky lost his 3-year-old father, lost his kind of fatherly love, followed his mother and grandmother, and came to a small dyeing workshop of his grandfather. From then on, the dark life came to Gorky's head. His grandfather had a bad temper. He often beat his grandmother and Gorky, causing a shadow of Gorky's young mind.
Later, Gorky met another Zhixin friend, little ioka. The two had a deep friendship. However, not often, the poor little zigang was killed by two fierce uncles. Gorky lost his friends. Yakov and Mikhail are the devil. oupai's wife is still busy every day to divide the family, and the brothers are not united at all. The two sons, Sasha, are also taught bad, and Gorky is everywhere. Afterwards, Gorky met the carpenter and became a friend. As a result, Gorky was driven away by his grandfather. Gorky got a stepdad and beat people very fiercely. After a few years, Gorky's grandmother died, and his mother died. Then he was driven out by his grandfather and made a living by garbage collection.
This "family of idiots" Grandfather Kash Lin is grumpy, well-behaved, greedy, and selfish. The two uncles, Mikhail and Yakov, are also crude and selfish market leaders, even the children had a warm invitation with them. Only a kind, kind, and emotional grandmother gives him a little comfort in this environment.
I like my grandmother in the text. She seems to have a special affinity. She has endless stories. And she loves her children so much. Even the two bad guys, Mikhail and Yakov, have not asked her grandfather how to severely punish them. How can a kind person like to have a war at home? In the face of a heartless call from her grandfather, she also endured it.
Gorky's childhood has nothing to remember except education and friendship! In joy, in sorrow, In the interweaving of love and hate, his childhood passes in such a rush. In reading, I found his love, thinking about his hate, and tasting the dark light in the dark.
What do we need, and what are our parents' "pearl on the Palm" and "Baby in the heart"? Can we compare them with Gorky's miserable childhood? The difference between ages is that, one is heaven, one is hell; the other is full of sunshine and the other is dark everywhere. Our childhood is hard to come!
Childhood is full of joy, childhood is warm everywhere, childhood is a memorable camera, childhood is full of heart, the teacher enthusiastically impart knowledge, the students discuss each other, we like a small seedling, in the face of spring breeze and rain-learn more and better knowledge, and thrive. in this sea of knowledge, we end our happy childhood and begin to mature.
We live in a socialist country that is full of humanity. There are no shoes, no beats, no hatred, no greed, no good manners, and no endless violence or scandal. people here are kind, pure, and optimistic, so our childhood is full of happiness and happiness. Gorky can become a famous generation in such a difficult environment, and we must study hard to realize our own life ideals.
2. Post reading in childhood
Maxim Gorky's childhood is world-famous. On an ordinary Sunday afternoon, I opened it. As the line of the book's black simplified Chinese characters suddenly rises and falls ......
The miserable childhood life of the hero, a Liao Sha, touched me: the four-year-old lost father, following the grief-stricken mother and kind grandmother to the domineering, near-bankruptcy little dyeing Square, the main grandfather's house, they often beat their grandfathers. But a kind grandmother kept him everywhere. At his grandfather's house, he met many "quiet" relatives, including two selfish, greedy, and desperate uncle mihailo and Yakov, there are two more cousins named Sarah. Simple and loving the "Little iegan" (Ivan) of aliosha, even though it is red and swollen, he uses his arm to block his grandfather from hitting aliosha. But he was so strong that he was able to survive when he was helping the 2xiaoyakov carry the cross ......
A's childhood was spent in a typical family of Russian residents: greedy, cruel, and ignorant. There was a fight between father and son, brother, and husband and wife; for the sake of property, we often quarrel and fight for small things ...... But in this dark family, there is a hardworking, strong, kind grandmother. She often gives... the full text>

Insects Reading Notes

Abstract: chinese idioms are related to termites, moles, dragonflies, moles, moles, moleys, moles, moles, saicus, moles, butterflies, moths, bees, ants, flies, insect groups such as mosquitoes, mosquitoes, and insects, it is the most common idiom involving bees, ants, and flies. Some insect names in idioms are currently not commonly used or have different meanings from the current insect names, such as growth, and chicken, this article explains. The article also analyzes the morphological and biological characteristics of insects reflected by idioms and some inscientific aspects of idioms.
Key words: Chinese idioms; insects; morphology; biology; Interpretation
Insects account for the largest proportion of all animals on the earth and are closely related to humans. This is also seen in Chinese idioms. Currently, the total number of Chinese idioms included in the dictionary is 2 ~ According to the author's statistics, about 40 thousand idioms are generated by the behavior, habits, or morphology of insects, which is much higher than those of other animals. This article describes the search and statistical results of Chinese idioms involving insects, and explains the special insect names in idioms and the morphological and biological characteristics of insects.
Proportion of insects and related idioms involved in idioms
After searching and summarizing more than 30000 Chinese idioms, it is found that the insects that can be viewed in Chinese idioms include: canonicus (termite), chinarmius, chinarus, and chinacus; the species of the Chinese; the bees and anticas of Apsara stack, and the flies, mosquitoes, species, and species of Apsara stack. About 255 idioms are involved in these groups. In addition, there are about 60 idioms involving "Insects" and "Insects.

For the above 255 idioms, the proportion of the involved groups is calculated. Result 1. (Because "Insects" are not necessarily insects, even insects cannot determine which groups they refer to. "Insects" generally refer to "worms" and cannot determine which groups they refer. So the idioms "Insects" or "worms" are not counted .)

2 Explanation of special insect names in idioms
Because Chinese idioms are mostly from ancient languages, some of them have different insect names, and are rarely known to modern people. This type of idioms is representative, and the names of insects are interpreted based on a variety of data.
2.1. "Boiling soup" refers to the meaning of the disturbance, such as the sound of the slogan and boiling soup.
2.2 different from the previous one, the "Yi" in this idiom should be the abbreviation of "Yi Mao", that is, "Yi Mao. A single axe is a single axe ". The "Axe" is the forefoot of the worm, because it is often held up in the shape of a person's axe, hence the name. "Feng" refers to the thorn of the hedgehog. The "Feng Yi Mao" is a metaphor for the small power.
The first moth in the 2.3 s is a kind of Chan, small, wide square head, and pattern. The first moth represents the beauty of a woman's face.
2.4.
2.5 Chu waist leads the "yellow" here refers to the "yellow" refers to the "yellow", that is, the worm larvae. In modern times, the worm generally refers to the larvae of the Golden Turtle. "Chu waist collar" describes a woman in slim shape, neck whitening.
2.6 butterfly complaints refer to "moles" or locusts. "Butterfly resentment" refers to the grief of homesickness. In the names of modern insects, the worm is a type of insects that are rare in China.
2.7 The frog-braised chicken is a type of insects that activity on vinegar or sour wine. The "frog and chicken" is a metaphor for a wide range of insights.
2.8 moth attach the bee here "Moth" through "ant ". "Moth attach bee" refers to gathering like an ant and a bee, which is described as different and messy.
Morphological, biological, and ecological characteristics of insects in Chinese Idioms
Chinese idioms use the morphological, biological, and ecological characteristics of insects to transform people or things. The majority of idioms use the biological characteristics of insects.
3.1 morphological characteristics of insects in Chinese Idioms
Some Chinese idioms clearly apply the morphological characteristics of some insects, such as the shape, body size, body color, head type, compound eye, Bill, tentacles, wings, and feet. It is not clear what insects or locations are used in a few insect-related idioms. The following is an example.
3.1.1 most insects in Chinese idioms are small in size. 3.1.2 indicates that some people and things are less than 3.1.3, and some of them are less than 3.1.5 ....... Remaining full text>

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.