CF1560E.Polycarp and String Transformation

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Polycarp has a string ss. Polycarp performs the following actions until the string ss is empty (tt is initially an empty string):

  • he adds to the right to the string tt the string ss, i.e. he does t=t+st = t + s, where t+st + s is a concatenation of the strings tt and ss;
  • he selects an arbitrary letter of ss and removes from ss all its occurrences (the selected letter must occur in the string ss at the moment of performing this action).

Polycarp performs this sequence of actions strictly in this order.

Note that after Polycarp finishes the actions, the string ss will be empty and the string tt will be equal to some value (that is undefined and depends on the order of removing).

E.g. consider ss="abacaba" so the actions may be performed as follows:

  • tt="abacaba", the letter 'b' is selected, then ss="aacaa";
  • tt="abacabaaacaa", the letter 'a' is selected, then ss="c";
  • tt="abacabaaacaac", the letter 'c' is selected, then ss="" (the empty string).

You need to restore the initial value of the string ss using only the final value of tt and find the order of removing letters from ss.

Polycarp 有一个字符串 ss。Polycarp 执行以下操作,直到字符串 ss 为空(初始时 tt 为空字符串):

  • 他将字符串 ss 添加到字符串 tt 的右侧,即执行 t=t+st = t + s,其中 t+st + s 表示字符串 tt 与 ss 的拼接;
  • 他任意选择 ss 中的一个字母,并从 ss 中删除该字母的所有出现(所选字母在执行此操作时必须在字符串 ss 中存在)。

Polycarp 严格按上述顺序执行这一系列操作。

注意:当 Polycarp 完成所有操作后,字符串 ss 将为空,而字符串 tt 将等于某个值(该值未定义,且取决于删除字母的顺序)。

例如,考虑 ss = "abacaba",则操作可能如下进行:

  • tt = "abacaba",选择字母 'b',然后 ss = "aacaa";
  • tt = "abacabaaacaa",选择字母 'a',然后 ss = "c";
  • tt = "abacabaaacaac",选择字母 'c',然后 ss = ""(空字符串)。

你需要仅利用最终的 tt 值来还原初始字符串 ss,并找出从 ss 中删除字母的顺序。

输入格式

The first line contains one integer TT (1≤T≤1041 \le T \le 10^4) — the number of test cases. Then TT test cases follow.

Each test case contains one string tt consisting of lowercase letters of the Latin alphabet. The length of tt doesn't exceed 5⋅1055 \cdot 10^5. The sum of lengths of all strings tt in the test cases doesn't exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 TT(1≤T≤1041 \le T \le 10^4)—— 表示测试用例的数量。随后是 TT 个测试用例。

每个测试用例包含一个字符串 tt,该字符串仅由拉丁字母的小写字母组成。字符串 tt 的长度不超过 5⋅1055 \cdot 10^5。所有测试用例中字符串 tt 的长度总和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case output in a separate line:

  • −1-1, if the answer doesn't exist;
  • two strings separated by spaces. The first one must contain a possible initial value of ss. The second one must contain a sequence of letters — it's in what order one needs to remove letters from ss to make the string tt. E.g. if the string "bac" is outputted, then, first, all occurrences of the letter 'b' were deleted, then all occurrences of 'a', and then, finally, all occurrences of 'c'. If there are multiple solutions, print any one.

对于每个测试用例,在单独的一行中输出:

  • 若答案不存在,输出 −1-1;
  • 否则输出两个由空格分隔的字符串。第一个字符串必须为 ss 的一个可能的初始值;第二个字符串必须为一个字母序列——表示为了将字符串 ss 变为字符串 tt,需要按该顺序依次删除 ss 中的字母。例如,若输出字符串为 "bac",则表示首先删除所有 'b' 字符,然后删除所有 'a' 字符,最后删除所有 'c' 字符。若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    7
    abacabaaacaac
    nowyouknowthat
    polycarppoycarppoyarppyarppyrpprppp
    isi
    everywherevrywhrvryhrvrhrvhv
    haaha
    qweqeewew

    输出#1

    abacaba bac
    -1
    polycarp lcoayrp
    is si
    everywhere ewyrhv
    -1
    -1

说明/提示

The first test case is considered in the statement.

题目描述中已考虑第一个测试用例。

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

首页