CF1281B Azamon Web Services

思路

贪心,找到将s至多交换一次所能得到的字典序最小的字符串,再与c比较。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n; cin >> n;
 6     while (n--)
 7     {
 8         string s, c;
 9         cin >> s >> c;
10         if (s < c) { cout << s << endl; continue; }
11         string t = s;
12         sort(t.begin(), t.end());
13         int n = s.length();
14         bool flg = false;
15         for (int i = 0; i < n; i++)
16         {
17             if (flg) break;
18             if (s[i] != t[i])
19             {
20                 for (int j = n - 1; j > i; j--)
21                 {
22                     if (s[j] == t[i])
23                     {
24                         swap(s[i], s[j]); flg = true; break;
25                     }
26                 }
27             }
28         }
29         if (s < c) cout << s << endl;
30         else cout << "---" << endl;
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/wangyiming/p/12046756.html