Several minor issues in Data Structure

Source: Internet
Author: User

1. What are the valid sequences of data output stacks?

Out-of-stack order problems. How many different out-of-stack sequences are there for a stack (infinite) of 1, 2, 3,... n?

Analysis: for each number, it is required to go to the stack once and go out once. We set the status of the incoming stack to '1' and the outbound stack to '0 '. All States of N numbers correspond to a 2n binary number consisting of n 1 and N 0. The number of operations waiting for the inbound stack is arranged in the order of 1 Gb/s n. The number B of the inbound stack is greater than or equal to the value a (A ≤ B) of the outbound stack ), therefore, the total number of output sequences = the number of 2n binary numbers consisting of n 1 and N 0 from the left and right scans. The cumulative number of 1 is not less than 0.

In the 2n-bit binary number, the number of N 1 values is C (2n, n), and the remaining N values of 1 are automatically set to 0. The number of solutions that subtract the number of non-conformities (from left to right scans, and the number of accumulative counts of 0 is greater than 1) is required.

The number of non-conformities is characterized by left-to-right scans, in a certain odd digit 2 m + 1, the cumulative count of m + 1 0 and the cumulative count of M 1 will first appear, and the subsequent 2 (n-m) -1 bit has n-M 1 and n-m-1 0. If the next 2 (n-m)-1 on the 0 and 1 swap, making it n-M 0 and n-m-1 1, result 1 2n digits consisting of n + 1 0 and N-1 1 are obtained, that is, an undesired number corresponds to an arrangement consisting of n + 1 0 and N-1 1.

In turn, any 2n binary number consisting of n + 1 0 and N-1 1, because the number of 0 is two more, 2n is an even number, therefore, the cumulative count of 0 must be greater than 1 on a certain odd digit. Similarly, 0 and 1 are swapped in the following parts to form a 2n digit consisting of n 0 and N 1, that is, the 2N digits of N + 1 0 and N-1 1 must correspond to an invalid number.

Therefore, the noncompliant 2n digits correspond to n + 1 0 and N-1 1 in a one-to-one arrangement.

Obviously, the number of non-conforming solutions is C (2n, n + 1 ). The total number of output sequences = C (2n, n)-C (2n, n + 1) = 1/(n + 1) * C (2n, n)

 

2. head and tail of a generalized table

Ls = (A, B), (C, D ));

Head (LS) = (a, B );

Tail (LS) = (c, d ));

Head is the first element to be taken out. It can be an atomic table or a sub-Table. Tail extracts a table composed of all elements except the first element, it can be an empty table. No matter how many elements are taken out, the resulting table is composed of these elements, that is, adding a layer () to the outside ().

Let's look at another example:

Ls = (A, B, C), (D, E, F ));

A = tail (LS) = (d, e, f ));

B = head (A) = (d, e, f); // tail (A) = ();

C = tail (B) = (E, F );

D = head (c) = E; // tail (c) = (f );

 

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.