Minimal string CodeForces, minimalcodeforces

Source: Internet
Author: User

Minimal string CodeForces, minimalcodeforces

Minimal string CodeForces-797C

There is a string s and an empty string t and u. Each operation can take out and delete the first character of s and put it at the end of t, or remove the last character of t and put it at the end of u. Make sure that the last s and t are empty strings. Evaluate the possible u with the smallest Lexicographic Order.

Analysis: the operation of this question is equivalent to "adding characters in s to the stack in the left-to-right order, and performing any number of outbound stacks after any number of inbound stacks ". Obviously, the final string length must be equal, so the minimum Lexicographic Order is to be as small as possible. Therefore, you can use an array to record the numbers of 'A' to 'Z' that have not yet been written into the stack, and find them in the order of 'A' to 'Z. The first step for finding a character c is: first, find all the characters greater than or equal to c at the top of the stack, output the stack and output. The subsequent operations are only performed when there is a c that has not yet been written into the stack: In the original string that has not yet been written into the stack, find c from the past and then, and add other characters to the stack, directly Output c (equivalent to directly output c from the stack after the stack is imported) until all c without the stack is gone. After finding 'Z', output the remaining characters in all stacks.

1 # include <cstdio> 2 # include <cstring> 3 char s [100100]; 4 int a [200], now, top; 5 char st [100100]; 6 // The top is mistakenly written as a char type, resulting in re 7 int main () 8 {9 scanf ("% s", s); 10 int I, len = strlen (s); 11 for (I = 0; I <len; I ++) 12 a [s [I] ++; 13 for (I = 'a'; I <= 'Z'; I ++) 14 {15 while (top> 0 & st [top] <= I) 16 {17 printf ("% c", st [top]); 18 top --; 19} 20 while (a [I]) 21 {22 while (s [now]! = I & now <len) 23 {24 st [++ top] = s [now]; 25 a [s [now] --; 26 now ++; 27} 28 while (s [now] = I & now <len) 29 {30 printf ("% c", I); 31 a [I] --; 32 now ++; 33} 34} 35} 36 while (top> 0) 37 {38 printf ("% c", st [top --]); 39} 40 return 0; 41}

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.