Gardener and the Capybaras (hard version) (Codeforces 1775A2)
Tags: Greedy, constructive algorithms

This is an hard version of the problem. The difference between the versions is that the string can be longer than in the easy version. You can only do hacks if both versions of the problem are passed.Kazimir Kazimirovich is a Martian gardener. He has a huge orchard of binary balanced apple trees.Recently Casimir decided to get himself three capybaras. The gardener even came up with their names and wrote them down on a piece of paper. The name of each capybara is a non-empty line consisting of letters "a" and "b".Denote the names of the capybaras by the lines $$$a$$$, $$$b$$$, and $$$c$$$. Then Casimir wrote the nonempty lines $$$a$$$, $$$b$$$, and $$$c$$$ in a row without spaces. For example, if the capybara's name was "aba", "ab", and "bb", then the string the gardener wrote down would look like "abaabbb".The gardener remembered an interesting property: either the string $$$b$$$ is lexicographically not smaller than the strings $$$a$$$ and $$$c$$$ at the same time, or the string $$$b$$$ is lexicographically not greater than the strings $$$a$$$ and $$$c$$$ at the same time. In other words, either $$$a \le b$$$ and $$$c \le b$$$ are satisfied, or $$$b \le a$$$ and $$$b \le c$$$ are satisfied (or possibly both conditions simultaneously). Here $$$\le$$$ denotes the lexicographic "less than or equal to" for strings. Thus, $$$a \le b$$$ means that the strings must either be equal, or the string $$$a$$$ must stand earlier in the dictionary than the string $$$b$$$. For a more detailed explanation of this operation, see "Notes" section.Today the gardener looked at his notes and realized that he cannot recover the names because they are written without spaces. He is no longer sure if he can recover the original strings $$$a$$$, $$$b$$$, and $$$c$$$, so he wants to find any triplet of names that satisfy the above property.  

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.The only line of a test case contains the string $$$s$$$ ($$$3 \le |s| \le 2 \cdot 10^5$$$) — the names of the capybaras, written together. The string consists of English letters 'a' and 'b' only.It is guaranteed that the sum of string lengths over all test cases does not exceed $$$4 \cdot 10^5$$$.

Output: For each test case, print three strings $$$a$$$, $$$b$$$ and $$$c$$$ on a single line, separated by spaces — names of capybaras, such that writing them without spaces results in a line $$$s$$$. Either $$$a \le b$$$ and $$$c \le b$$$, or $$$b \le a$$$ and $$$b \le c$$$ must be satisfied.If there are several ways to restore the names, print any of them. If the names cannot be recovered, print ":(" (without quotes).

Note: A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds:   $$$x$$$ is a prefix of $$$y$$$, but $$$x \ne y$$$;  in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has the letter 'a', and the string $$$y$$$ has the letter 'b'. Now let's move on to the examples.In the first test case, one of the possible ways to split the line $$$s$$$ into three lines —is "b", "bb", "a".In the third test case, we can see that the split satisfies two conditions at once (i.e., $$$a \le b$$$, $$$c \le b$$$, $$$b \le a$$$ and $$$b \le c$$$ are true simultaneously).

Sample Inputs:
5
bbba
aba
aaa
abba
abbb

Sample Outputs:
b bb a
a b a
a a a
ab b a
a bb b

Solution (Python):
for s in [*open(0)][1:]:
    i = 2 - 4 * (s[1] > 'a')
    print(s[0], s[1:i], s[i:-1])

Solution (Python):
for s in [*open(0)][1:]:
    i = 2 - 4 * (s[1] > 'a')
    print(s[0], s[1:i], s[i:-1])

Solution (Python):
""""""
import os
from io import BytesIO, IOBase
import sys

def main():
    for _ in range(int(input())):
        s = input()
        if 'b' not in s:
            print('a', 'a' * (len(s) - 2), 'a')
        else:
            p = s.find('b')
            if p == 0:
                print('b', s[1], s[2:])
            elif p == len(s) - 1:
                print(s[:-2], s[-2], 'b')
            else:
                print(s[:p], s[p:-1], s[-1])
    return
BUFSIZE = 1048576

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
    return sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()