Greedy: The smallest problem of dictionary order

Source: Internet
Author: User

<title>Greedy: The smallest problem of dictionary order</title> Greedy: The smallest problem of dictionary order
Given the string s of length n, construct a string t of length n. At first, T is an empty string, followed by repeated operations 1. Remove one character from the head of S and add to the tail of T 2. Remove a character from the tail of s and add it to the end of T to find the string example of a dictionary that is as small as possible:  n = 6  s = "ACDBCB"    constructs T      t:a            S:CDBCB      t:ab           s:cdbc< C10/>T:ABC          s:cdb    # Note Here the choice of the direction of C, choose the wrong is not the smallest      t:abcb         s:cd      t:accbc      s:d t:abcbcd
   s:

Look at the algorithm to achieve it, it's easy to understand, anyway, I see it.

// createtime:2015-04-07 21:55:59#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <algorithm>#include <queue>using namespace STD;string s;string T;void Solve() {CIN >> S;int Len= S.length ();int L= 0;int R= len-1;int Sub= 0; while(L <= R) {BOOL  Left= 0; for(int I= 0; L+i <= R; i++) {if(S[l+i] < s[r-i]) {left = 1; Break; }Else if(S[l+i] > S[r-i]) {left = 0; Break; }        }if(left)        {t[sub++] = s[l++]; }Else{t[sub++] = s[r--]; }} T[sub] =' + ';//    cout << t << Endl; Don't understand why not outputcout << t.c_str () << Endl;}int Main(void) {solve ();return0;}

Greedy: The smallest problem of dictionary order

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.