Editorial for Biến đổi (TS10 LQĐ, Đà Nẵng 2021)
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lời giải:
.Đầu tiên ta nhận xét rằng điều ta quan tâm là thao tác xóa và thêm vào 2 đầu của một dãy số gồm các số từ 1->8.
.DEQUE là cấu trúc dữ liệu có đầy đủ các tính chất như vậy, bạn có thể thêm vào cả 2 đầu cũng như xóa ở cả 2 đầu của một dãy số(DEQUE khá dễ hiểu cơ chế).
Các bạn nào chưa từng biết đến DEQUE thì các bạn có thể tìm hiểu trên VNOI.
Code tham khảo:
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("hellodking.txt", "r", stdin);
#else
// online submission
#endif
// freopen("factorial.inp","r",stdin);
// freopen("factorial.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
deque<int> q;
for (int i = 1; i <= 8; i++)
q.push_back(i);//Đẩy các số lần lượt vào deque theo thứ tự từ 1 đến 8.
for (int i = 0; i < (int)s.size(); i++)
if (s[i] == 'R'){
q.push_front(q.back());//Chúng ta đưa phần tử cuối deque về đầu deque.
q.pop_back();//Sau đó loại bỏ phần tử cuối deque.
}
else{
q.push_back(q.front());//Chúng ta đưa phần tử đầu deque về cuối deque.
q.pop_front();//Sau đó loại bỏ phần tử đầu deque.
}
while (!q.empty()){
cout << q.front();//Đẩy các số ra khỏi deque theo thứ tự từ đầu đến cuối ta được kết quả.
q.pop_front();
}
}
Comments