Best cow line code (c)
Address: http://blog.csdn.net/caroline_wendy
Question: Given the string s with a length of N, you must construct a string T with a length of N. perform the following operations repeatedly.
Delete a character from the S header and put it at the end of T;
Delete a character from the end of S and put it at the end of T;
The objective is to construct a string T with the smallest Lexicographic Order Possible.
UseGreedy Algorithm, Continuous selectionS minimum character at the beginning and end, Put t, ifEqual, Then againInner searchTo find the smallest internal device.
Code:
/** Main. CPP ** created on: 2014.7.17 * Author: Spike * // * eclipse CDT, GCC 4.8.1 */# include <stdio. h> # include <limits. h >#include <utility> # include <queue> # include <algorithm> using namespace STD; Class program {static const int max_n = 10000; int n = 6; char s [max_n + 1] = "acdbcb"; public: void solve () {int A = 0, B = N-1; while (A <= B) {bool left = false; For (INT I = 0; A + I <= B; I ++) {// If (s [A + I] <s [B-I]) {left = true; break ;} else if (s [A + I]> S [B-I]) {left = false; break;} If (left) putchar (s [A ++]); else putchar (s [B --]);} putchar ('\ n') ;}}; int main (void) {program P; p. solve (); Return 0 ;}
Output:
ABCBCD