Polycarp and String Transformation

链接

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

  • he adds to the right to the string t the string s s s, i.e. he does t = t + s t=t+s t=t+s, where t + s t+s t+s is a concatenation of the strings t t t and s s s;
  • he selects an arbitrary letter of s s s and removes from s s s all its occurrences (the selected letter must occur in the string s 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 s s s will be empty and the string t t t will be equal to some value (that is undefined and depends on the order of removing).

E.g. consider s = " a b a c a b a " s="abacaba" s="abacaba" so the actions may be performed as follows:

t = " a b a c a b a " t="abacaba" t="abacaba", the letter ′ b ′ 'b' b is selected, then s = " a a c a a " s="aacaa" s="aacaa";
t = " a b a c a b a a a c a a " , t="abacabaaacaa", t="abacabaaacaa", the letter ′ a ′ 'a' a is selected, then s = " c " s="c" s="c";
t = " a b a c a b a a a c a a c " t="abacabaaacaac" t="abacabaaacaac", the letter ′ c ′ 'c' c is selected, then s = " " s="" s="" (the empty string).
You need to restore the initial value of the string s s s using only the final value of t t t and find the order of removing letters from s s s.

Input

The first line contains one integer T ( 1 ≤ T ≤ 104 ) T (1≤T≤104) T(1T104) — the number of test cases. Then T T T test cases follow.

Each test case contains one string t t t consisting of lowercase letters of the Latin alphabet. The length of t t t doesn’t exceed 5 ⋅ 105 5⋅105 5105. The sum of lengths of all strings t t t in the test cases doesn’t exceed 5 ⋅ 105 5⋅105 5105.

Output

For each test case output in a separate line:

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

Example

input

7
abacabaaacaac
nowyouknowthat
polycarppoycarppoyarppyarppyrpprppp
isi
everywherevrywhrvryhrvrhrvhv
haaha
qweqeewew

output

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

Note

The first test case is considered in the statement.

思路

首先假设每个字符串 t t t 都可以构造出一个满足题意的 s s s 串。从后到前扫描一遍 t t t 串,找出每个字符最后出现的顺序,将这个顺序反转,就是删除字符的顺序。

统计一遍 t t t 中每个字符出现的次数,这个次数除以它在删除顺序中的下标,就是它在 s s s 串中出现的次数。这样就可以得到 s s s 串中每个字母的出现的次数。

从前到后扫描一遍 s s s 串,统计每个字母出现的次数,若找到一个位置,使得字母出现次数满足要求,那么就用这段前缀,根据删除字母的顺序,构造一个 t ′ t' t,若 t ′ = = t t'==t t==t,则满足题意,否则不存在这样的 s s s 串。

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
string s,ord; int T,n;

bool check(int x){
	vector<bool> vis(n);
	string ss=s.substr(0,x+1);
	for(int i=0;i<(int)ord.size();i++)
		for(int j=0;j<=x;j++){
			if(vis[j]==1||s[j]==ord[i]) vis[j]=1;
			else ss.push_back(s[j]);
		}
	return ss==s;
}

void solve(){
	cin>>s; n=s.size();
	ord.clear();
	vector<int> v(26),pre(26),cnt(26);
	for(int i=n-1;i>=0;i--){
		int t=s[i]-'a';
		if(v[t]==0){ v[t]=1; ord.push_back(t+'a'); }
	}
	reverse(ord.begin(),ord.end());
	for(int i=0;i<n;i++) cnt[s[i]-'a']++;
	for(int i=0;i<(int)ord.size();i++) cnt[ord[i]-'a']/=(i+1);
	for(int i=0;i<n;i++){
		pre[s[i]-'a']++;
		if(pre==cnt){
			if(check(i)){ s=s.substr(0,i+1);
				return (void)(cout<<s+" "+ord<<"\n");
			}else return (void)(cout<<"-1\n");
		}
	}
	cout<<"-1\n";
}

int main(){
	ios::sync_with_stdio(false);
	for(cin>>T;T;T--) solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值