Programming Algorithm-best cow line code (c)

Source: Internet
Author: User
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









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.