Gardener and the Capybaras

文章描述了一个关于火星园丁记下的三个水豚名字的字符串问题。园丁记得这些名字满足特定的字典序条件,但现在他忘记了原始的分隔。给定合并后的字符串,任务是找出任何满足条件的名字组合。输入包含多个测试用例,每个用例包含一个由a和b组成的字符串。解决方案涉及查找特定模式来恢复原始名字。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 题目
  • 解析
  • 代码


题目

A2. Gardener and the Capybaras (hard version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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≤b and c≤bc≤b are satisfied, or b≤ab≤a and b≤cb≤c are satisfied (or possibly both conditions simultaneously). Here ≤≤ denotes the lexicographic "less than or equal to" for strings. Thus, a≤ba≤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.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.

The only line of a test case contains the string ss (3≤|s|≤2⋅1053≤|s|≤2⋅105) — 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⋅105.

Output

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≤b and c≤bc≤b, or b≤ab≤a and b≤cb≤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).


 

一、思路

对于一个ab字符串我们只需要判断能否从去除首位的中间字符串中找到‘a’。如果能答案就是‘a’之前的字符串加上'a',加上‘a'之后的字符串。如果不能就是首位两个字符加上中间字符。

二、代码如下

#include <string>
#include <iostream>
#include <algorithm>
#include<queue>
#include<set>
#include<cstring>
#include<vector>

using namespace std;
using ll = long long;

ll n, m;
string s;

void slove() {
    bool flag = false;
    int x = 0;
    for (int i = 1; i < s.size() - 1; i++) {
        if (s[i] == 'a') {
            flag = true;
            x = i;
            break;
        }
    }
    if (flag) {
        for (int i = 0; i < s.size(); i++) {
            if (i == x) {
                cout << ' ' << s[i] << ' ';
            }
            else {
                cout << s[i];
            }
        }
    }
    else {
        cout << s[0] << ' ';
        for (int i = 1; i < s.size() - 1; i++) {
            cout << s[i];
        }
        cout << ' ' << s[s.size() - 1];
    }
}

int main() {
    int T;
    for (cin >> T; T--; cout << endl) {
        cin >> s;
        slove();
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值