• C. Phone Numbers


    http://codeforces.com/problemset/problem/940/C

    And where the are the phone numbers?

    You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.

    It's guaranteed that the answer exists.

    Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}.

    String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than aband is not lexicographically smaller than a.

    Input

    The first line of input contains two space separated integers n and k (1 ≤ n, k ≤ 100 000) — the length of s and the required length of t.

    The second line of input contains the string s consisting of n lowercase English letters.

    Output

    Output the string t conforming to the requirements above.

    It's guaranteed that the answer exists.

    Examples
    input
    Copy
    3 3
    abc
    output
    aca
    input
    Copy
    3 2
    abc
    output
    ac
    input
    Copy
    3 3
    ayy
    output
    yaa
    input
    Copy
    2 3
    ba
    output
    baa
    Note

    In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of sis as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca.

     思维题

    1.s.size()>len 直接往后加最小的字符

    2.其他情况,从后往前找(不是最大字符)的第一个字符,将它改为字典序下一个字符,然后将它后面的字符全改为最小字符

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
    template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
    template <class T> inline T min(T a, T b, T c, T d) {
      return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d) {
      return max(max(a, b), max(c, d));
    }
    #define scanf1(x) scanf("%d", &x)
    #define scanf2(x, y) scanf("%d%d", &x, &y)
    #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
    #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int maxn = 10005;
    // name*******************************
    string s, s1;
    int len;
    vector<char> vec;
    bool vis[maxn];
    int n;
    // function******************************
    
    //***************************************
    int main() {
      //    ios::sync_with_stdio(0);
      //    cin.tie(0);
      // freopen("test.txt", "r", stdin);
      //  freopen("outout.txt","w",stdout);
      cin >> n >> len;
      cin>>s;
      For(i, 0, s.size() - 1) {
        if (vis[s[i]] == 0) {
          vec.pb(s[i]);
        }
      }
      sort(vec.begin(), vec.end());
      s1 = s;
      // cout<<len<<" "<<s1.size()<<endl;
      if (len > s1.size()) {
        cout<<s1;
        For(i, 1, len - s1.size())cout<<vec.front();
        return 0;
      }
      s1 = s.substr(0, len);
      FFor(i, len - 1, 0) {
        char c = s1[i];
        if (c == vec.back())
          continue;
        FFor(j, vec.size() - 2, 0) {
          if (c == vec[j]) {
            s1[i] = vec[j + 1];
            For(k, i + 1, len - 1) { s1[k] = vec.front(); }
            cout << s1;
            return 0;
          }
        }
      }
    
      return 0;
    }
  • 相关阅读:
    记录
    Remote System Upgrade With Cyclone III Devices
    【Diary】Noip2020 游记
    【Diary】CSP-S 2020 游记
    【Diary】JZSC 2020 旅 游 记(迫真
    【题解】Luogu P2671 【求和】
    51nod 1153 选择子序列
    Luogu P4116 Qtree3
    Luogu P4114 Qtree1
    【Contest】Nowcoder 假日团队赛1 题解+赛后总结
  • 原文地址:https://www.cnblogs.com/planche/p/8627423.html
Copyright © 2020-2023  润新知