Stamp cover design word Solitaire

xiaoxiao2021-03-06  70

Question 1: Description of the stamp cover design: give a given envelope, only the N-stamps are allowed, calculated in the case of a given K (N K <= 40) stamp (assuming all stamps is enough), How to design the face value of the stamp, you can get the maximum max, so that each postage value between 1__max can be obtained. For example: n = 3, k = 2, if the face value is 1 point, 4 points, each of the postage values ​​between 1 point __6 can be obtained (of course 8 points, 9 points, and 12 points) : If the face value is 1 point, 3 points can be obtained at each postage value between 1 minute -7. When n = 3, k = 2, 7 points is the continuous postage maximum, so Max = 7, the face value is 1 minute, 3 points, respectively.

Sample: Inputn = 3 k = 2Output1 3max = 7 Running: n = 7 n = 7k = 3 k = 41 8 13 1 5 24 37MAX = 69 MAX = 165n = 10 n = 5K = 3 k = 51 10 26 1 4 9 31 51max = 146 MAX = 126 Stamp Problem: (2000 Level 1 Shu Xingming) #define N 32767 # Define M0 20 # Define M 1500INT X [M0], Y [M 1], BX [M0 ], MAX = 0, N0, K0; Void Stamp (INT I, INT R) {INT Z [M 1], J, K; For (j = 0; j <= x [i-2] * (N0 -1); J ) IF (y [j] k0) {IF (R-1 > max) {MAX = R-1; for (j = 1; j <= k0; j ) bx [j] = x [j];} return;} for (k = 1; k <= m; k ) Z [k] = y [k]; for (j = x [i-1] 1; j <= r; j ) {x [i] = j; stamp (i 1, r); for (K = 1; k <= m; k ) y [k] = z [k];}} main () {INT i; printf ("INPUT: N0, K0: / N"); scanf ("% D% D) ", & n0, & k0); for (i = 1; i <= m; i ) y [i] = n; for (i = 0; i <= k0; i ) x [i] = 0; x [1 ] = 1; y [0] = 0; stamp (2, 1); Printf ("OUTPUT: / N"); for (i = 1; i <= k0; i ) Printf ("% 4d", BX [ I]); Prin TF ("/ nmax =% D / N", MAX);} Seven, the word Solitaire word Solitaire is a similar game with the idioms that we often play, now we know a set of words, and give a letter, The longest "Dragon" starting with this letter (each word is up to two in the "Dragon"), and the coincidence is partially combined with two words, for example: beast and astonish, if connected It turns into beastonish, and the adjacent two parts cannot be present, such as between the AT and Atide cannot be connected.

Enter: The first behavior of the input is a separate integer n (n <= 20) represents the number of words. The following N rows have a word each line, the last behavior of the input is a single character, indicating the "Dragon" starting letter. You can assume that the "Dragon" starting with this letter must exist. Output: Just output the longest "Dragon" length starting with this letter. Sample: INPUT5ATTouchCHEATCHOOSetAtPut23 (Connected "Dragon" is: atoucheatactactouchoose problem analysis: This question is mainly to test the search, as for the use of width priority search or depth priority search, which is familiar to which algorithm is familiar with which algorithm is adopted. In fact, the two algorithms have their own advantages and disadvantages. The width is prioritized, and the storage space is high; while the depth priority search does not have internal memory, but the running speed is relatively slow. Data Structure: There is a judgment on whether two words can be connected in the search in the search, so recorded by a neighbor matrix. The program finally outputs the longest length of the dragon, but we must not only know if a word can be connected to the dragon tail, but also know if the word can be connected to the words of the dragon, will connect to the dragon tail After the length of the dragon has increased. Therefore, the adjacent matrix is ​​defined as follows: 0 Word j cannot be connected after LINK [i] [j] = LEN word J can be connected to the word i, the length of the dragon after the connection is only required to calculate the longest dragon The length, so the queue element only needs to record the length of the dragon, and does not require the string of the dragon corresponding to, so that a large number of storage space can be saved. In addition, the topic specifies that each word is used up to twice, so the searches should be recorded in the current node, and each word is used several times. The data structure is defined as follows: Define struct node {int State [MAX]; Record the word of the dragon in the node constitute int Last; record the last word number INT LEN of the dragon in the node; record the length of the Dragon } Algorithm Analysis: The algorithm for calculating the link array is as follows: for (i = 1; i <= n; i ) FRO (j = 1; j <= n; j ) {link [i] [j] = 0; false = 0; len = 1; / * len Represents word i and word J overlapping length * / while ((len strlen (Word [i]) Find = 1; Else LEN = LEN 1;} if Find Linkk [i] [J] = Strlen (Word [J]) - Len;} First Meeting All Letters as the Specified Letter Put the queue, as an initial queue, and then take an element from the team to an element, will extend the new knot (the dragon after connecting a new word) is increasing in the tail, until no new endpoint Finally, the entire queue is scanned, and the length of the longest dragon in the queue can be obtained.

Algorithm for width priority search: Hean = 1; / * Hean is a header pointer, Tail point to the tail * / while (head 0) {for (j = 1) J <= n; j ) q [tail] .state [j] = q [head] .state [j]; q [tail] .state [i] = q [tail] .state [i] 1; Q [tail] .last = i; q [tail] .len = q [head] .lend link [q [head] .last] [i]; tail ; head ;}

Checkered square map (n <= 8) with N * N (N <= 8), we fill in the positive integer in some of the other squares, and add numbers 0. As shown: To the right 000 problem analysis: The problem given by the question is N K <= 40, whether it is dynamic planning; in fact, this question has obvious post-grace, the difference in the previous scheme makes a face value Different stamps, dynamic planning is not established. The method of solving such problems is generally considered to solve the search, and the problem can be solved is far from the requirements of the problem (N and K must not exceed 6), which can be said to be a trap in the condition. Even for smaller N and K, you must have the right idea to have the right idea. The first star value is a stamp is necessary, can be fixed; after which each surface value is added, the amount of the current can achieve will be recorded through the addition of K-1, and all the schemes are obtained. Optimal solution. This is the basic framework of the algorithm. Data Structure: During the search process, the record mode of the data is critical. If only the amount can be reached, this record is basically no use. If you add a stamp next time, you should not know if you can add a new stamp from this amount, you must all compete, waste a lot. Time; So the least records to reach a face value for a few stamps, it is convenient to quickly determine the feasibility when adding a new stamp. Also searching a stamp surface value should also be noted that it should be added from the previous face value to 1 until the maximum amount of continuous achievements plus 1. Excellent solutions outside this range can not be considered: Excessive causes discontinuous; if less than the previous side value, the front and rear face value can be exchanged, still obtaining a stamp surface value sequence. The program is recorded in the search process in the program, the face value of each stamp during the search process, the array result record the current best solution, the array D records the current situation, and each face value is at least a few stamps can be reached. MAXC means that the current situation cannot be The minimum face value reached. Program List: 1. String Edit From the keyboard into a string (length <= 40 characters), and end with '.'. For example: 'this is a book.' Is now editing, editing features: D: Deleting a character, the way the command is: D a where A is deleted, for example: D s means deleting characters 's', If there is multiple 's', delete the first appearance, as the result of the deletion in the above example is:' Thi is a book.'i: Insert a character, the command format is: i a1 a2 where A1 represents inserted into the specified character In front, A2 represents the character to be inserted. For example, I S D indicates that character 'd' in front of the specified character 's', if there is a plurality of 's' in the original string, inserted in front of the last character. As in the above example: the original string: 'this is a book.' Insert: 'this IDs a book.'r: Replace a character, the command format is: R A1 A2 where the A1 is replaced, A2 is a character, If there are multiple A1 in the original string, it should be replaced. For example: the original string: 'this is a book.' Enter a command: r OE replacement string is: 'this is a beek.' During the editing process, the prompt is given when the specified characters do not exist. information.

转载请注明原文地址:https://www.9cbs.com/read-90391.html

New Post(0)