CF1775A2.Gardener and the Capybaras (hard version)

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 aa, bb, and cc. Then Casimir wrote the nonempty lines aa, bb, and cc 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 bb is lexicographically not smaller than the strings aa and cc at the same time, or the string bb is lexicographically not greater than the strings aa and cc at the same time. In other words, either a≤ba \le b and c≤bc \le b are satisfied, or b≤ab \le a and b≤cb \le c are satisfied (or possibly both conditions simultaneously). Here ≤\le denotes the lexicographic "less than or equal to" for strings. Thus, a≤ba \le b means that the strings must either be equal, or the string aa must stand earlier in the dictionary than the string bb. 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 aa, bb, and cc, so he wants to find any triplet of names that satisfy the above property.

这是一个该问题的困难版本。两个版本的区别在于,本版本中的字符串长度可能比简单版本更长。只有当两个版本的问题均通过后,才允许进行 Hack。

卡齐米尔·卡齐米罗维奇是一位火星园丁,他拥有一大片二叉平衡苹果树果园。

最近,卡西米尔决定为自己养三只水豚。这位园丁甚至为它们取好了名字,并将名字写在了一张纸上。每只水豚的名字均为一个非空字符串,仅由字母 “a” 和 “b” 构成。

记这三只水豚的名字分别为字符串 aa、bb 和 cc。随后,卡西米尔将这三个非空字符串 aa、bb、cc 按顺序拼接(中间不加空格)写成一个字符串。例如,若三只水豚的名字分别为 "aba"、"ab" 和 "bb",则园丁所写的字符串即为 "abaabbb"。

园丁记得一个有趣的性质:字符串 bb 要么字典序不小于 aa 和 cc(即同时满足 a≤ba \le b 与 c≤bc \le b),要么字典序不大于 aa 和 cc(即同时满足 b≤ab \le a 与 b≤cb \le c);当然,两种情况也可能同时成立。此处 ≤\le 表示字符串的字典序“小于或等于”关系。也就是说,a≤ba \le b 意味着两字符串要么完全相等,要么在字典序中 aa 排在 bb 之前。关于该运算的更详细说明,请参见“注释”部分。

今天,园丁查看自己的笔记时,发现由于名字之间未加空格,他已无法还原出原始的三个名字。他现在甚至不确定是否还能唯一恢复出原始字符串 aa、bb 和 cc,因此他希望找出任意一组满足上述性质的三元组 (a,b,c)(a, b, c)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The only line of a test case contains the string ss (3≤∣s∣≤2⋅1053 \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⋅1054 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例仅有一行,包含字符串 ss(3≤∣s∣≤2⋅1053 \le |s| \le 2 \cdot 10^5)——即水豚的名字连写而成的字符串。该字符串仅由英文字母 'a' 和 'b' 组成。

保证所有测试用例的字符串长度之和不超过 4⋅1054 \cdot 10^5。

输出格式

For each test case, print three strings aa, bb and cc on a single line, separated by spaces — names of capybaras, such that writing them without spaces results in a line ss. Either a≤ba \le b and c≤bc \le b, or b≤ab \le a and b≤cb \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).

对于每个测试用例,在一行中输出三个字符串 aa、bb 和 cc,以空格分隔——即水豚的名字,使得将它们不加空格拼接后得到字符串 ss。必须满足以下条件之一:

  • a≤ba \le b 且 c≤bc \le b,或
  • b≤ab \le a 且 b≤cb \le c。

若存在多种恢复名字的方式,输出任意一种即可。若无法恢复名字,则输出 :((不带引号)。

输入输出样例

  • 输入#1

    5
    bbba
    aba
    aaa
    abba
    abbb

    输出#1

    b bb a
    a b a
    a a a
    ab b a
    a bb b

说明/提示

A string xx is lexicographically smaller than a string yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx \ne y;
  • in the first position where xx and yy differ, the string xx has the letter 'a', and the string yy 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 ss 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≤ba \le b, c≤bc \le b, b≤ab \le a and b≤cb \le c are true simultaneously).

字符串 xx 在字典序上小于字符串 yy,当且仅当以下条件之一成立:

  • xx 是 yy 的前缀,但 x≠yx \ne y;
  • 在 xx 与 yy 首次出现差异的位置上,字符串 xx 对应的字母为 'a',而字符串 yy 对应的字母为 'b'。

现在我们来看示例。

在第一个测试用例中,将字符串 ss 拆分为三行的一种可能方式是 "b"、"bb"、"a"。

在第三个测试用例中,我们可以看到该拆分同时满足两个条件(即 a≤ba \le b、c≤bc \le b、b≤ab \le a 和 b≤cb \le c 同时成立)。

输入解题思路,AI测评打分。不知道怎么写?

首页