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;
    }
    
  • 相关阅读:
    提高网站访问速度的34个方法
    ASP.NET MVC3学习笔记四(Controller)
    ASP.NET MVC3 读书笔记一(Razor视图)
    DataSet DataTable DataReader dataAdapter区别
    EF Code First(约定配置)
    asp.net中APPlication、Session和Cookie的区别
    ASP.NET MVC3 读书笔记三(数据注解Dataannotation和验证)
    一步步构建大型网站架构
    ASP.NET MVC3 读书笔记二(HtmlHelper)
    Sql 查询语句总结
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12104850.html
Copyright © 2011-2022 走看看