C. Phone Numbers
思维题
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("***********\n"); #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; }