題目連結
非常好的一道題!
觀察,如果 $k = 1$ 的話,你可以得到什麼呢?如果 $k = 2$ 的話,你可以得到什麼呢?
$k = 1 \rightarrow$ 你可以做 logical left shift
$k = 2 \rightarrow$ 你可以對任意兩點做 swap !
AC Code
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 | // :%s/^ \* //g
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;
// define data structures here
#endif
static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();
class Solution
{
public:
    string orderlyQueue(string S, int k)
    {
        if (k == 1) {
            string best = S;
            int sz = S.size();
            for (int i = 0; i < sz; i++) {
                rotate(S.begin(), next(S.begin()), S.end());
                best = min(best, S);
            }
            /*
            for (int i = 0; i < sz; i++) {
                if (S < best)
                    best = S;
                char c = S.front();
                S = S.substr(1);
                S.push_back(c);
            }
            */
            return best;
        } else {
            sort(S.begin(), S.end());
            return S;
        }
    }
};
#ifdef LOCAL
int main()
{
    return 0;
}
#endif
 |