B. Azamon Web Services

 

Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.

After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!

To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.

Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!

Given the string ss representing Jeff's product name and the string cc representing his competitor's product name, find a way to swap at most one pair of characters in ss (that is, find two distinct indices ii and jj and swap sisi and sjsj) such that the resulting new name becomes strictly lexicographically smaller than cc, or determine that it is impossible.

Note: String aa is strictly lexicographically smaller than string bb if and only if one of the following holds:

  • aa is a proper prefix of bb, that is, aa is a prefix of bb such that aba≠b;
  • There exists an integer 1imin(|a|,|b|)1≤i≤min(|a|,|b|) such that ai<biai<bi and aj=bjaj=bj for 1j<i1≤j<i.
Input

The first line of input contains a single integer tt (1t15001≤t≤1500) denoting the number of test cases. The next lines contain descriptions of the test cases.

Each test case consists of a single line containing two space-separated strings ss and cc (2|s|5000,1|c|50002≤|s|≤5000,1≤|c|≤5000). The strings ss and cc consists of uppercase English letters.

It is guaranteed that the sum of |s||s| in the input is at most 50005000 and the sum of the |c||c| in the input is at most 50005000.

Output

For each test case, output a single line containing a single string, which is either

  • the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than cc. In case there are many possible such strings, you can output any of them;
  • three dashes (the string "---" without quotes) if it is impossible.
Example
input
Copy
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
output
Copy
AMAZON
---
APPLE
Note

In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".

It is impossible to improve the product's name in the second test case and satisfy all conditions.

In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 11092019;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        string a, b;
        cin >> a >> b;
        vector<vector<int>> v(26);
        for (int i = 0; i < a.size(); i++)
        {
            v[a[i] - 'A'].push_back(i);
        }
        for (int i = 0; i < a.size(); i++)
        {
            int fg = 0;
            for (int j = 0; j < a[i] - 'A' && fg==0; j++)
            {
                if (v[j].size() && v[j].back()>i)
                {
                    fg = v[j].back();
                }
            }
            if (fg)
            {
                swap(a[i], a[fg]);
                break;
            }
        }
        if (a < b)
            cout << a << endl;
        else
            cout << "---" << endl;
    }

    return 0;
}

 

posted @ 2020-05-05 16:18  DeaL57  阅读(179)  评论(0编辑  收藏  举报