Typical common algorithms (C ++)

Source: Internet
Author: User
I. Fast sorting void qsort (INT X, int y) // The data to be sorted is stored in a [1] .. in the array a [n], {int H = x, r = y; int M = A [(x + y)> 1]; // take the value of the position in the center while (H <r) {While (A [H] <m) h ++; // It is smaller than the value of the position in the center, loop until you find a while (A [R]> m) r --; // It is larger than the value at the center, loop until you find an IF (H <= r) {int temp = A [H]; // If H <= r, exchange a [H] And a [R] a [H] = A [R]; A [R] = temp; H ++; r --; // these two sentences are indispensable.} If (r> X) qsort (x, R); // note the following, the tail pointer runs to the first half if (H <Y) qsort (H, Y); // note that the header pointer runs to the second half} call: qsort (1, N) the elements in array a are ordered. Suitable for N sorting. II. Bubble sorting. Void paopao (void) // The data to be sorted is stored in a [1] .. in the [n] array, {for (INT I = 1; I <n; I ++) // number of times the loop (bubble) is controlled, n counts, needs n-1 bubbles for (Int J = 1; j <= n-I; j ++) // compare adjacent pairs if (a [J] <A [J + 1]) {int temp = A [J]; A [J] = A [J + 1]; A [J + 1] = temp ;}} or void paopao (void) // The data to be sorted is stored in a [1] .. in the [n] array, {for (INT I = 1; I <n; I ++) // number of times the loop (bubble) is controlled, n counts, needs n-1 bubbles for (Int J = n-I; j> = 1; j --) // compare adjacent pairs if (a [J] <A [J + 1]) {int temp = A [J]; A [J] = A [J + 1]; A [J + 1] = temp;} call: paopao (), applicable to N Ratio Small sort 3. The value range of the bucket sort void bucketsort (void) // A is known. For example, a <= Cmax. {Memset (Tong, 0, sizeof (Tong); // The bucket initialization for (INT I = 1; I <= N; I ++) // read n count {int ACIN> A; Tong [a] ++;} // Add 1 for (INT I = 1; I <= Cmax; I ++) {If (Tong [I]> 0) // when the number of trees in the bucket is greater than 0, it indicates that I has appeared Tong [I] times, otherwise, I while (Tong [I]! = 0) {Tong [I] --; cout <I <<<''' ;}} bucket sorting applies to sorting of values of the keywords to be sorted in a known range. 4. Merge (return) and sort void merge (int l, int M, int R) // merge [l, m] and [M + 1, r] Two ordered intervals {int B [101]; // use a new array B to merge two ordered subintervals into an ordered interval, note the int h, T, K; k = 0 for the size of the B array; // the pointer H = L for the new array B; t = m + 1; // Let H point to the first element of the first interval, and t point to the first element of the second interval. While (h <= m) & (T <= r) // when the pointer H and T are not at the end of the interval, copy the elements of the two intervals to the new array {k ++; // Add 1 if (a [H] <A [T]) to the new array pointer. {B [k] = A [H]; H ++;} // copy the first interval element to the new array else {B [k] = A [T]; t ++;} // copy the second range element to the new array} while (H <= m) {k ++; B [k] = A [H]; h ++;} // if the first interval is not copied, copy the rest in the new array while (T <= r) {k ++; B [k] = A [T]; t ++;} // if the second interval is not copied, copy the rest to the new array for (int o = 1; O <= K; O ++) // copy the elements in the new array back to the original range. These two consecutive intervals are changed to ordered intervals. A [L + O-1] = B [O];} void mergesort (INT X, int y) // sort the interval [x, y] by merging two rows {int mid; if (x> = y) return; Mid = (x + y)/2; // calculates the [x, y] interval, the mid point in the middle, and mid converts X, the y interval is divided into two parts: mergesort (x, mid); // merge the first part of mergesort (Mid + 1, Y); // merge Merge (X, mid, Y); // merge the two segments that have already been sorted} merge and sort the segments to apply the grouping idea, turning a big problem into two small problems. The second is the idea of sub-governance. 5. Binary Search int find (int x, int y, int m) // in the [x, y] interval, find the element subscript with the keyword equal to m {int head, tail, mid; head = x; tail = y; Mid = (x + y)/2); // obtain the subscript of the intermediate element if (a [Mid] = m) return mid; // If the intermediate element value is m, return the intermediate element subscript mid if (head> tail) return 0; // If x> Y, the search fails, returns 0 if (M> A [Mid]) // If M is greater than the middle element, searches in the second half and returns return find (Mid + 1, tail) in the second half ); else // If M is smaller than the intermediate element, search in the first half interval and return the result return find (Head, mid-1 );} vi. High-Precision addition # include <iostream> # include <cstring> using namespace STD; int Main () {string str1, str2; int A [250], B [250], Len; // the size of the array determines the maximum number of digits of the computed precision int I; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); CIN> str1> str2; // enter two strings a [0] = str1.length (); // obtain the length of the first string for (I = 1; I <= A [0]; I ++) // convert the first string to an integer and store it in array a [I] = str1 [A [0]-I]-'0 '; B [0] = str2.length (); // obtain the second string length for (I = 1; I <= B [0]; I ++) // convert each digit of the second string to an integer and store it in array B [I] = str2 [B [0]-I]-'0 '; len = (a [0]> B [0]? A [0]: B [0]); // obtain the maximum length of the two strings for (I = 1; I <= Len; I ++) // perform bitwise addition and process carry {A [I] + = B [I]; A [I + 1] + = A [I]/10; A [I] % = 10;} Len ++; // below is the result of removing the highest bit 0 and then outputting it. While (A [Len] = 0) & (LEN> 1) Len --; for (I = Len; I> = 1; I --) cout <A [I]; return 0;} Note: when the two numbers are added, the number of digits in the result should be one more than the one in the two numbers. VII. High-Precision subtraction # include <iostream> using namespace STD; int compare (string S1, string S2); int main () {string str1, str2; int A [250], B [250], Len; int I; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B )); cin> str1> str2; A [0] = str1.length (); for (I = 1; I <= A [0]; I ++) A [I] = str1 [A [0]-I]-'0'; B [0] = str2.length (); for (I = 1; I <= B [0]; I ++) B [I] = str2 [B [0]-I]-'0'; If (compare (str1, str2) = 0) // if the value is greater than or equal to, perform bitwise subtraction and handle bitwise. {For (I = 1; I <= A [0]; I ++) {A [I]-= B [I]; if (a [I] <0) {A [I + 1] --; A [I] + = 10 ;}} A [0] ++; while (A [A [0] = 0) & (A [0]> 1) A [0] --; for (I = A [0]; I> = 1; I --) cout <A [I]; cout <Endl;} else {cout <'-'; // if the value is less than, the negative number for (I = 1; I <= B [0]; I ++) // perform bitwise subtraction, greatly decreasing {B [I]-= A [I]; if (B [I] <0) {B [I + 1] --; B [I] + = 10 ;}} B [0] ++; while (B [B [0] = 0) & (B [0]> 1) B [0] --; for (I = B [0]; i> = 1; I --) cout <B [I]; cout <Endl;} return 0;} int compare (string S1, String S2) // compare the size of a string (two numbers). If the value is greater than or equal to 0, the return value is less than 1. {If (s1.length ()> s2.length () return 0; // compare the length first. If (s1.length () <s2.length ()) return 1; for (INT I = 0; I <= s1.length (); I ++) // compare the values of one character and one character with the same length. {If (S1 [I]> S2 [I]) return 0; If (S1 [I] <S2 [I]) return 1;} return 0; // If the length is the same and each bit is the same, 0 is returned, indicating equal}. When performing subtraction, you must first determine the size of the two strings and determine whether to output a negative number, the bitwise Subtraction Method is followed. Handle the bitwise. 8. High-Precision multiplication # include <iostream> # include <cstring> using namespace STD; int main () {string str1, str2; int A [250], B [250], c [500], Len; // two numbers within 250 bits are multiplied by INT I, j; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); CIN> str1> str2; A [0] = str1.length (); for (I = 1; I <= A [0]; I ++) A [I] = str1 [A [0]-I]-'0'; B [0] = str2.length (); for (I = 1; I <= B [0]; I ++) B [I] = str2 [B [0]-I]-'0'; memset (C, 0, sizeof (c); for (I = 1; I <= A [0]; I ++) // perform bitwise multiplication and process carry. Statement. For (j = 1; j <= B [0]; j ++) {C [I + J-1] + = A [I] * B [J]; c [I + J] + = C [I + J-1]/10; C [I + J-1] % = 10 ;} len = A [0] + B [0] + 1; // remove the highest bit 0, and then output while (C [Len] = 0) & (LEN> 1) Len --; // why Len> 1 ?? For (I = Len; I> = 1; I --) cout <C [I]; return 0;} Note: two numbers are multiplied, the number of digits in the result should be the number of digits in the two numbers and minus 1. Optimization: hichina # include <iostream> # include <cstring> using namespace STD; void num1 (INT s [], string ST1); int A [2501], B [2501], C [5002]; // 2500-bit hexadecimal multiplication, that is, 10000-bit decimal multiplication, can be performed here. Int main () {string str1, str2; int Len; CIN> str1> str2; memset (A, 0, sizeof (a); memset (B, 0, sizeof (B); memset (C, 0, sizeof (c); num1 (A, str1); // start str1 from the second bit, every four bits are stored in array a num1 (B, str2); // start str2 from the nth bit, and each four bits are stored in array B for (INT I = 1; I <= A [0]; I ++) // perform bitwise multiplication and process carry. Here, the hexadecimal carry for (Int J = 1; j <= B [0]; j ++) {C [I + J-1] + = A [I] * B [J]; c [I + J] + = C [I + J-1]/10000; C [I + J-1] % = 10000;} Len = A [0] + B [0]; // A [0] and B [0] store the four-digit processing bits of each number while (C [Len] = 0 )& & (LEN> 1) Len --; // remove the high 0 and output the highest cout <C [Len]; for (INT I = len-1; I> = 1; I --) // restores each of the remaining bits into four outputs {If (C [I] <1000) cout <'0 '; if (C [I] <100) cout <'0'; If (C [I] <10) cout <'0 '; cout <C [I];} cout <Endl; return 0;} void num1 (INT s [], string ST1) // This function is used to set the string ST1, store the length of ST1 in array s {int K = 1, Count = 1; s [0] = st1.length, save a length variable for (INT I = s [0]-1; I> = 0; I --) // starts from the nth bit, process each bit {If (count % 4 = 0) {s [k] + = (ST1 [I]-'0') * 100 0; if (I! = 0) K ++;} If (count % 4 = 1) s [k] = (ST1 [I]-'0 '); if (count % 4 = 2) s [k] + = (ST1 [I]-'0') * 10; If (count % 4 = 3) s [k] + = (ST1 [I]-'0') * 100; count ++;} s [0] = K; // The number of digits that store the array, it is the number of digits in the hexadecimal format after processing by four bits. Return;} 9. High-Precision Division (not mentioned) 10. Create a prime number table void maketable (int x) by using the 'struct' method. // create a prime number table prim within 'x'. Prim [I] is 0, indicates that I is a prime number, and 1 indicates that it is not a prime number {memset (Prim, 0, sizeof (prim); // initializes the prime number table prim [0] = 1; prim [1] = 1; prim [2] = 0; // use the Evaluate Method to Calculate the prime number table within X for (INT I = 2; I <= x; I ++) if (prim [I] = 0) {Int J = 2 * I; while (j <= x) {prim [J] = 1; j = J + I ;}} for those algorithms, it is often necessary to determine the problem of prime numbers. To create a prime number table, we can achieve the goal of permanent. 11. The depth-first search for void DFS (int x) \ is an example of depth-first traversal of an image. {Cout <x <''; \ Access x vertex visited [x] = 1; \ mark accessed for (int K = 1; k <= N; k ++) \ performs In-depth priority search on node K adjacent to vertex x but not accessed. If (A [x] [k] = 1) & (visited [k] = 0) DFS (k );} 12. Search void BFS (void) by breadth first // traverse graph G by breadth first, n vertices, numbered 1 .. n. Note: The graph is not necessarily connected {// use the auxiliary queue Q and access tag array visited. For (V = 1; v <= N; V ++) visited [v] = 0; // tag array initialization for (V = 1; v <= N; V ++) if (visited [v] = 0) {// v has not accessed int H = 1, R = 1; // set the null auxiliary queue Q visited [v] = 1; // vertex v for access marking cout <v <''; // access vertex v q [R] = V; // v enters the queue while (H <= r) // when the queue is not empty, the loop {int TMP = Q [H]; // The queue Header element leaves the queue and is assigned to TMP for (Int J = 1; j <= N; j ++) if (visited [J] = 0) & (A [TMP] [J] = 1 )) {// J is an inaccessible adjacent vertex of TMP, visited [J] = 1; Access mark for J: cout <j <''; Access j r ++; // Add 1q [R] = J at the end of the team; // J into the team} // end-If H ++;} // end-Wh Ile} 13. Traverse void preorder (int x) in the forward, middle, and backward directions of Binary Trees // The first traversal of Binary Trees {If (x = 0) return; cout <X; // first access the root preorder (A [X]. LD); // traverse the left subtree preorder (A [x] first. rd); // Finally, first traverse the right subtree of the root} void inorder (int x) // The middle traversal of the Binary Tree {If (x = 0) return; preorder (A [X]. LD); // traverse the left subtree cout of the root in the middle order <X; // then access the root preorder (A [X]. rd); // right subtree} void reorder (int x) // return of the Binary Tree {If (x = 0; preorder (A [X]. LD); // preorder (A [X]. rd); // traverse the right subtree cout of the root in the descending order <X; // Then access root} fourteen, the tree is converted to the binary tree algorithm fifteen, the binary tree sorting tree sixteen, and The Harman tree void Haff (void) // build the Harman tree {for (INT I = n + 1; I <= 2 * n-1; I ++) // generate n-1 nodes in sequence {int L = fmin (I-1 ); // search for the node number with the smallest weight l A [I]. lchild = L; // use L as the left child of node I A [l]. father = I; // modify the parent node of L to I int r = fmin (I-1); // find the number of the sub-weight r a [I]. rchild = r; // use L as the right child of node I A [R]. father = I; // modify the parent node of R to I a [I]. DA = A [l]. da + A [R]. da; // merge L and J nodes to generate new node I} int fmin (int K) // find the smallest weight number in 1 to k {int mins = 0; for (int s = 1; S <= K; s ++) if (A [m INS]. da> A [s]. da) & (A [s]. father = 0) // A [s]. father = 0, indicating that this node is not another node mins = s; // the child, not equal to 0 indicates that this node has been used. Return mins;} void inorder (int x) // recursively generate the Harman encoding {if (a [X]. father = 0) {A [X]. code = ";} // root node if (a [A [X]. father]. lchild = X) A [X]. code = A [A [X]. father]. code + '0'; if (a [A [X]. father]. rchild = X) A [X]. code = A [A [X]. father]. code + '1'; if (a [X]. lchild! = 0) inorder (A [X]. lchild); // recursively generates the left subtree if (A [X]. lchild = 0) & (A [X]. rchild = 0) // output leaf node cout <A [X]. da <':' <A [X]. code <Endl; if (a [X]. rchild! = 0) inorder (A [X]. rchild); // recursively generate the right subtree} 17. query the int getfather (int x) set. // perform non-recursion to find the number of the root node of the x node {While (X! = Father [x]) x = Father [X]; return X;} int getfather (int x) // recursively calculate the number of the root node of the x node {If (x = Father [x]) return X; else return getfather (father [x]);} int getfather (int x) // calculate the root node number of the x node recursively and compress the path at the same time. {int P = x; while (P! = Father [p]) // After the loop ends, p is the root node P = Father [p]; while (X! = Father [x]) // compress the path from the x node along the X parent node {int temp = Father [x]; // temporary storage x parent node father [x] = P before modification; // point the parent node of X to PX = temp;} return P;} int getfather (int x) // recursively calculate the root node number of the x node and compress the path {If (x = Father [x]) return X; else {int temp = getfather (father [x]); father [x] = temp; return temp ;}} void merge (int x, int y) // merge X, y two nodes {int x1, x2; X1 = getfather (x); // obtain the X parent node X2 = getfather (y ); // obtain the parent node if (x1! = X2) Father [X1] = x2; // merge if two parent nodes are different. Note: the root of the X and Y nodes is merged .} 18. Prime algorithm void prime (void) // The prim algorithm calculates the Minimum Spanning Tree. elist [I] is the number group of edge sets, and a [I] [J] is <I, j>. Edge is the struct type. {For (INT I = 1; I <= n-1; I ++) // initialize the edge set formed from node 1 to other n-1 nodes {elist [I]. from = 1; elist [I]. to = I + 1; elist [I]. W = a [1] [I + 1];} For (INT I = 1; I <= n-1; I ++) // determine the n-1 edge in sequence {int M = I; for (Int J = I + 1; j <= n-1; j ++) // when the I-th edge is determined, find the smallest edge from I + 1 to n-1 in sequence if (elist [J]. W <elist [M]. w) M = J; If (M! = I) // If the smallest edge is not the I edge, the {edge TMP = elist [I]; elist [I] = elist [m]; elist [m] = TMP;} For (Int J = I + 1; j <= n-1; j ++) // update the minimum distance between the I + 1 and n-1 edges. {If (elist [J]. w> A [elist [I]. to] [elist [J]. to]) elist [J]. W = a [elist [I]. to] [elist [J]. to] ;}}for (INT I = 1; I <= n-1; I ++) // calculate the value of the minimum spanning tree ans = ans + elist [I]. w;} if the edge is required to form the minimum spanning tree, when updating the smallest distance between the I + 1 to n-1 edge to the generated tree (the rough part in the code above), you must add elist [J]. from = elist [I]. to; statement, that is, when updating the weight value, it should also be a new starting point. The prime algorithm is suitable for dense graphs with not too many vertices. It is not suitable for sparse graphs with a large number of vertices. 19. Dijkstra algorithm void Dijkstra (int x) // calculate the shortest path from node X to each node {memset (VIS, 0, sizeof (VIS); // initialize, vis [I] = 0 indicates that the source point is not obtained from node I; otherwise, vis [x] = 1; Pre [x] = 0; // initialize the source point. For (INT I = 1; I <= N; I ++) // initialize other points. If (I! = X) {dis [I] = G [x] [I]; Pre [I] = x;} For (INT I = 1; I <= n-1; I ++) // for n-node graphs, the shortest distance from X to n-1 nodes is required. {int M = big; // The maximum number of virtual nodes is big = 99999999; int K = x; For (Int J = 1; j <= N; j ++) // find a source point in the unobtained node to the point with the smallest distance if (vis [J] = 0 & M> dis [J]) {M = dis [J]; k = J;} vis [k] = 1; // think: what if k = x? The following points are described. For (Int J = 1; j <= N; j ++) // use the currently found node to update the shortest path from unobtained node to X if (vis [J] = 0) & (DIS [k] + G [k] [J] <dis [J]) {dis [J] = dis [k] + G [k] [J]; // Update pre [J] = K; // Save the front node, so that the following path }}note: DIS [I] indicates the shortest distance from X to I, And pre [I] indicates the frontend node of the I node. 20. kruscal algorithm void qsort (int x, int y) // sort the edge set quickly {int H = x, r = Y, M = elist [(H + r)> 1]. w; while (H <r) {While (elist [H]. W <m) h ++; while (elist [R]. w> m) r --; If (H <= r) {edge TMP = elist [H]; elist [H] = elist [R]; elist [R] = TMP; h ++; r -- ;}}if (x <r) qsort (x, R); If (H <Y) qsort (H, Y );} int getfather (int x) // find the root node and compress the path. Recursive Implementation is used here. {If (x = Father [x]) return X; else {int F = getfather (father [x]); father [x] = f; return F ;}} void merge (int x, int y) // merge the X and Y nodes. In this question, X and Y are two root nodes. {FATHER [x] = y;} void kruscal (void) {int sum = 0, ANS = 0; qsort (1, t ); // sort t edge values in ascending order by weight (INT I = 1; I <= T; I ++) {int X1 = getfather (elist [I]. from); // int X2 = getfather (elist [I]. to); // obtain the root of the tree where the end point of the edge of entry I is located if (x1! = X2) {sum ++; merge (x1, x2); ans + = elist [I]. W ;}// not in the same set, merge, that is, the I edge can be selected. If (sum> N-1) break; // The n-1 edge has been determined, and the minimum spanning tree has been generated. you can exit the loop in advance} If (sum <n-1) cout <"impossible" <Endl; // The n-1 side cannot be determined from the T side, indicating that the Minimum Spanning Tree else cout cannot be generated <ans <Endl ;} the cruise Kar algorithm only uses edge sets and does not use the adjacent matrix of the graph. Therefore, when the number of nodes in the graph is large, the input data is the edge information, we need to consider using the kruscal algorithm. For island countries, we need to select this algorithm. If we use the prim algorithm, we also need to open a two-dimensional array to represent the graph's Adjacent matrix. For the data of 10000 points, obviously, it cannot be tolerated in space. 21. floyed algorithm void floyed (void) // A [I] [J] indicates the shortest path length from node I to node J. The initial value is <I, j>. {For (int K = 1; k <= N; k ++) // if the number of nodes in the enumeration center does not exceed K, F [I] [J] Shortest Path Length, K is equivalent to the stage for (INT I = 1; I <= N; I ++) // I in DP. J is the node I to node J, equivalent to the state in DP for (Int J = 1; j <= N; j ++) if (A [I] [J]> A [I] [k] + A [k] [J]) A [I] [J] = A [I] [k] + A [k] [J]; // This is the decision, and the addition and without the intermediate point, the shortest path length of a graph without a negative weight loop is obtained by using the shortest value}. The floyed algorithm can be used to determine whether node I and node J are connected. 22. 01 backpack question n indicates the number of items. W [I] indicates the weight of the I-th item, and C [I] indicates the value of the I-th item, V is the maximum weight of a backpack. The stateful transition equation f [I] [J] = max {f [I-1] [J], F [I-1] [J-W [I] + C [I]}. F [I] [J] indicates the maximum value obtained when the carrying capacity of the first I item is J. Obviously, F [N] [v] is the request. The boundary condition is f [0] [s] = 0, S = 0 ,..., V. For (INT I = 1; I <= N; I ++) // For (Int J = 0; j <= V; j ++) // enumeration status. Of course, this can also be written as: For (Int J = V; j> = 0; j --) {f [I] [J] = f [I-1] [J]; // If (F [I] [J] <F [I-1] [J-W [I] + C [I]) f [I] [J] = f [I-1] [J-W [I] + C [I]; // select the I-th item} cout <F [N] [v] <Endl; // output the result. Optimization: using a one-dimensional array, the first I-1 stage and the first stage data exist. For (INT I = 1; I <= N; I ++) // For (Int J = V; j> = 0; j --) // enumeration status, of course, this can also be written as: For (Int J = V; j> = 0; j --) {f [J] = f [J]; // do not select the I-th item, this statement can be omitted. If (j> W [I]) & (f [J] <F [J-W [I] + C [I]) f [J] = f [J-W [I] + C [I]; // select the I-th item} cout <F [v] <Endl; // output the result. Before and after optimization, we can easily find that the optimized code is actually based on the original basic code, which reduces the one-dimension of the phase and ensures the correctness of the result during enumeration, the enumeration sequence can only be V to 0, but not 0 to v. Why? Is to ensure that f [J-W [I] is the value of the I-1 phase when the J state of Phase I is obtained. Further Optimization: In the code above, you can also write the for (Int J = V; j> = W [I]; j --), the following judgment condition j> = W [I] can be omitted. 23. The difference between the full backpack problem and the 01 backpack problem is that a full backpack can be selected multiple times as long as the weight of the backpack permits for any item I, that is, in decision-making, you can select 0, 1, 2 ,..., V/W [I. State transition equation f [I] [J] = max {f [I-1] [J], F [I-1] [J-W [I] + C [I], f [I-1] [J-2 * W [I] + 2 * C [I],…, F [I-1] [J-K * W [I] + K * C [I]}. K = 0, 1, 2 ,..., V/W [I]. F [I] [J] indicates the maximum value obtained when the carrying capacity of the first I item is J. Obviously, F [N] [v] is the request. The boundary condition is f [0] [s] = 0, S = 0 ,..., V. For (INT I = 1; I <= N; I ++) // For (Int J = 0; j <= V; j ++) // enumeration status. Of course, this can also be written as: For (Int J = V; j> = 0; j --) {f [I] [J] = f [I-1] [J]; // The case where k = 0 is used as the initial value of F [I] [J, then at k = ,..., Find the maximum value in V/W [I] For (int K = 1; k <= V/W [I]; k ++) if (F [I] [J] <F [I-1] [J-K * W [I] + K * C [I]) f [I] [J] = f [I-1] [J-K * W [I] + K * C [I]; // select the I-th item} cout <F [N] [v] <Endl; // output the result. 24. Multi-attribute backpack question 25, multi-backpack question 26, longest non-drop (rising) Sub-sequence question F [I] indicates starting from 1st, the longest ascending subsequence ending with the number of I. State transition equation: F [I] = max {f [J]} + 1 (1 ≤ j ≤ I-1, 1 ≤ I ≤ n, a [I] ≥ A [J]) critical status: F [1] = 1; 27. Longest Common subsequence problem f [I] [J] indicates the maximum number of common subsequences of the first I character and the first J character of the second. State transition equation: F [I-1] [J-1] (if a [I] = B [J]) f [I] [J] = max {f [I-1] [J], F [I] [J-1]} + 1 (if a [I] = B [J]) critical status: F [0] [J] = 0, F [I] [0] = 0

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.