Codeforces Round #761 (Div. 2) A. Forbidden Subsequence

题目链接:Problem - A - Codeforces

You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc.

Find string S′, the lexicographically smallest permutation of S such that T is not a subsequence of S′.

String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.

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

  • a is a prefix of b, but a≠b;
  • in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input

Each test contains multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a string S (1≤|S|≤100), consisting of lowercase English letters.

The second line of each test case contains a string T that is a permutation of the string abc. (Hence, |T|=3).

Note that there is no limit on the sum of |S| across all test cases.

Output

For each test case, output a single string S′, the lexicographically smallest permutation of S such that T is not a subsequence of S′.

Example

input

7
abacaba
abc
cccba
acb
dbsic
bac
abracadabra
abc
dddddddddddd
cba
bbc
abc
ac
abc

output

aaaacbb
abccc
bcdis
aaaaacbbdrr
dddddddddddd
bbc
ac

Note

In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.

In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.

In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence.

题意:给定字符串s与t,求出s的最小组合情况t不是s的子序列

思路:先把s的所有字母从小到大排列,如果t == "abc"那么就把所有的‘b’,‘c’交换

#include<bits/stdc++.h>
using namespace std;

int arr[30];

int main(){
	int t;
	cin >> t;
	string s, tt;
	while(t--){
		memset(arr, 0, sizeof(arr));
		cin >> s >> tt;
		string ss = "";
		for(int i = 0; i < s.length(); i++){
			arr[s[i] - 'a']++;
		}
		bool f = 0;;
		for(int l = 0; l < 26; l++){
			if(ss[ss.length() - 1] == 'a' && tt == "abc" && l == 1 && f == 0){
				l++;
				while(arr[l]){
					ss += (l + 'a');
					arr[l]--;
				}
				l = 0;
				f = 1;
			}
			while(arr[l]){
				ss += (l + 'a');
				arr[l]--;
			}
			
		}
		cout << ss << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值