C. Minimal string

Codeforces
IDCF797C
Time1000ms
Memory256MB
Difficulty
data structuresgreedystrings
English · Original
Chinese · Translation
Formal · Original
Petya recieved a gift of a string _s_ with length up to 105 characters for his birthday. He took two more empty strings _t_ and _u_ and decided to play a game. This game has two possible moves: * Extract the **first** character of _s_ and append _t_ with this character. * Extract the **last** character of _t_ and append _u_ with this character. Petya wants to get strings _s_ and _t_ empty and string _u_ lexicographically minimal. You should write a program that will help Petya win the game. ## Input First line contains non-empty string _s_ (1 ≤ |_s_| ≤ 105), consisting of lowercase English letters. ## Output Print resulting string _u_. [samples]
Petya 收到了一个长度最多为 $10^5$ 个字符的字符串 $s$ 作为生日礼物。他取了两个空字符串 $t$ 和 $u$,并决定玩一个游戏。这个游戏有两种可能的操作: Petya 希望最终让字符串 $s$ 和 $t$ 都变为空,而字符串 $u$ 字典序最小。 你需要编写一个程序帮助 Petya 赢得这个游戏。 第一行包含一个非空字符串 $s$($1 ≤ |s| ≤ 10^5$),由小写英文字母组成。 请输出最终得到的字符串 $u$。 ## Input 第一行包含一个非空字符串 $s$($1 ≤ |s| ≤ 10^5$),由小写英文字母组成。 ## Output 请输出最终得到的字符串 $u$。 [samples]
**Definitions** Let $ s \in \Sigma^* $ be the input string, where $ \Sigma = \{a, b, \dots, z\} $ and $ 1 \leq |s| \leq 10^5 $. Let $ t = \varepsilon $, $ u = \varepsilon $ be initial empty strings. **Operations** At each step, one of the following moves is performed: 1. Move the first character of $ s $ to the end of $ t $. 2. Move the last character of $ t $ to the end of $ u $. **Objective** Transform $ s $ and $ t $ to empty strings, and maximize the lexicographical minimality of $ u $. **Output** The lexicographically minimal string $ u \in \Sigma^* $ achievable through any sequence of valid moves.
Samples
Input #1
cab
Output #1
abc
Input #2
acdb
Output #2
abdc
API Response (JSON)
{
  "problem": {
    "name": "C. Minimal string",
    "description": {
      "content": "Petya recieved a gift of a string _s_ with length up to 105 characters for his birthday. He took two more empty strings _t_ and _u_ and decided to play a game. This game has two possible moves: *   E",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF797C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Petya recieved a gift of a string _s_ with length up to 105 characters for his birthday. He took two more empty strings _t_ and _u_ and decided to play a game. This game has two possible moves:\n\n*   E...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Petya 收到了一个长度最多为 $10^5$ 个字符的字符串 $s$ 作为生日礼物。他取了两个空字符串 $t$ 和 $u$,并决定玩一个游戏。这个游戏有两种可能的操作:\n\nPetya 希望最终让字符串 $s$ 和 $t$ 都变为空,而字符串 $u$ 字典序最小。\n\n你需要编写一个程序帮助 Petya 赢得这个游戏。\n\n第一行包含一个非空字符串 $s$($1 ≤ |s| ≤ 10^5$),由小写英...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ s \\in \\Sigma^* $ be the input string, where $ \\Sigma = \\{a, b, \\dots, z\\} $ and $ 1 \\leq |s| \\leq 10^5 $.  \nLet $ t = \\varepsilon $, $ u = \\varepsilon $ be initial empty string...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments