zoukankan      html  css  js  c++  java
  • Codeforces Round #607 (Div. 2) B. Azamon Web Services

    链接:

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

    题意:

    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.

    思路:

    从前往后找一个后面有值比他小的位置,交换一下,就是能得到的最小的字典序

    代码:

    #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;
    }
    
  • 相关阅读:
    关于python的类成员方法允许静态调用的机制
    一个pyqt5动态加载ui+多线程+信号刷新界面的例子
    一个经典的redis队列简单解决方案
    关于连表查询的执行计划
    electron nativefier打包网址 electron-winstaller制作安装包
    MySQL 8.0 字符串比较特殊案例小结
    MySQL SELECT_LEX与subselect 执行 源码阅读笔记
    MySQL 8.0 QueryResolver 源码笔记
    MySQL 8.0 Optimizer 源码笔记
    MySQL Item 源码阅读笔记
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12104850.html
Copyright © 2011-2022 走看看