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;
    }
    
  • 相关阅读:
    ubuntu修改主机名称+修改终端显示目录和计算机名称
    google 搜索 PPT
    就为在YouTube上下载个视频
    读书笔记 ---- 《嵌入式系统技术》
    WARNING: Unable to open an initial console
    Ubuntu ftp服务器搭建 + UltraEdit编辑FTP文件
    51Nod 1079 中国剩余定理【模板题】
    Leetcode 11 Container with most water【双指针】
    kuangbin专题六 最小生成树【从入门到熟练】【5题】
    kuangbin专题十二 基础DP1【从入门到熟练】【10题】
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12104850.html
Copyright © 2011-2022 走看看