#P9348. 小园香径独徘徊

    ID: 9969 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串贪心洛谷原创后缀自动机 SAMO2优化后缀数组 SA洛谷月赛

小园香径独徘徊

Background

Wandering along a quiet, deep path, you pick up fragments of memory and put them into two long pockets.

After collecting them all and pouring them out, what kind of story will they form?

Problem Description

There are two strings S,TS,T. Initially, SS is given and TT is the empty string. Each time, you may perform one of the following three operations until SS becomes the empty string:

  1. Delete the first character of SS, and insert this character at the beginning of TT.
  2. Delete the first character of SS, and insert this character at the end of TT.
  3. Delete the last character of SS, and insert this character at the beginning of TT.

a3 wants to know, after SS becomes the empty string, the lexicographically smallest TT that can be formed.

Input Format

This problem contains multiple test cases.

The first line contains an integer QQ, which denotes the number of test cases.

For each test case, one line contains a string SS.

Output Format

For each test case, output one line containing a string, which is the lexicographically smallest TT that can be formed.

2
ababdca
dcbcadb
aaabbdc
abbcdcd

Hint

[Sample 1 Explanation]

  • For ababdca\texttt{ababdca}, perform operations of type 1,2,1,2,2,2,11,2,1,2,2,2,1 in order, and you can obtain aaabbdc\texttt{aaabbdc}.
  • For dcbcadb\texttt{dcbcadb}, perform operations of type 1,1,1,2,3,1,21,1,1,2,3,1,2 in order, and you can obtain abbcdcd\texttt{abbcdcd}.

[Constraints and Agreements]

This problem uses bundled testdata.

  • Subtask 1 (5 points): SS consists of at most two distinct characters.
  • Subtask 2 (10 points): S12\sum |S|\le 12.
  • Subtask 3 (15 points): S100\sum |S|\le 100.
  • Subtask 4 (25 points): S3×103\sum |S|\le 3\times 10^3.
  • Subtask 5 (20 points): S2×105\sum |S|\le 2\times 10^5.
  • Subtask 6 (25 points): no special restrictions.

For 100%100\% of the data, 1Q3×1051\le Q\le 3\times 10^5, 1S1061\le |S|\le 10^6, 1S2×1061\le \sum |S|\le 2\times 10^6, and SS consists only of lowercase letters.

Translated by ChatGPT 5