Codeforces Round #607 (Div. 2) B. Azamon Web Services

link:

https://codeforces.com/contest/1281/problem/B

Meaning of the questions:

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 s representing Jeff's product name and the string c representing his competitor's product name, find a way to swap at most one pair of characters in s (that is, find two distinct indices i and j and swap si and sj) such that the resulting new name becomes strictly lexicographically smaller than c, or determine that it is impossible.

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

a is a proper prefix of b, that is, a is a prefix of b such that a≠b;
There exists an integer 1≤i≤min(|a|,|b|) such that ai<bi and aj=bj for 1≤j<i.

Ideas:

From front to back to find a value smaller than behind his position, swap, is to get the smallest lexicographically

Code:

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

string s, c;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        cin >> s >> c;
        for (int i = 0;i < (int)s.length();i++)
        {
            int tmp = i;
            for (int j = s.length()-1;j >= i;j--)
                if (s[j] < s[tmp]) tmp = j;
            if (tmp != i)
            {
                swap(s[tmp], s[i]);
                break;
            }
        }
        if (s < c)
            cout << s << endl;
        else
            cout << "---" << endl;
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/12104850.html