<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