注意边界情况。
#include<bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int music[N]; int m; int hh, tt, idx; inline void move2(char c) { if (c == 'U') { if (idx > 1) idx--; else idx = m; } else if (c == 'D') { if (idx < 3) idx++; else idx = 1; } } inline void move(char c) { if (idx == 1 && c == 'U') { hh = m - 3, tt = m, idx = m; } else if(idx == m && c == 'D') { hh = 1, tt = 4, idx = 1; } else if (idx == hh && c == 'U') { hh--, tt--, idx = hh; } else if (idx == tt && c == 'D') { hh++, tt++, idx = tt; } else { if (c == 'U') { idx--; } else if (c == 'D') { idx++; } } //cout << "hh: " << hh << " tt: " << tt << " idx: " << idx << endl; } inline void init(int n) { hh = 1, tt = n < 4 ? n : 4, idx = 1; for (int i = 1; i <= n; i++) { music[i] = i; } } int main() { int n; string opt; for (; cin >> n >> opt; memset(music, 0, sizeof(music))) { m = n; init(n); if (n < 4) for (const auto &c : opt) move2(c); else for (const auto &c : opt) move(c); for (int i = hh; i <= tt; i++) cout << i << " "; cout << endl << idx << endl; } return 0; }