2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
There is an overturned barrel in the kindergarten.Linear structure, you can only put the basketball in the right side of the bucket, but you can take it out from the left or right side of the bucket. Each basketball has a separate number. The teacher can put one or more basketballs in at a time, and the children can take the basketball out from the left or right side of the bucket. When there is only one basketball in the bucket, it can only be taken out from the left side of the bucket.
For example, if the teacher puts in 5 basketballs numbered 1, 2, 3, 4, and 5 in order, the children can take out basketballs numbered "1, 2, 3, 4, 5" or "3, 1, 2, 4, 5" in order, but cannot take out basketballs numbered "5, 1, 3, 2, 4". The scenario for taking out basketballs numbered "3, 1, 2, 4, 5" is: put in 1, 2, 3 in succession -> take out 3 from the right -> take out 1 from the left -> take out 2 from the left -> put in 4 -> take out 4 from the left -> put in 5 -> take out 5 from the left. For simplicity, we use L to represent taking out basketballs from the left and R to represent taking out basketballs from the right. The sequence of taking out basketballs at this time is "RLLLL";
The numbers in the first row are the numbers of the basketballs that the teacher puts in one by one;
The numbers in the second row are the numbers of the basketballs to be checked to see if they can be taken out in the order they were put in;
The basketball numbers are separated by commas.
For each basketball retrieval sequence, if it is indeed possible to obtain it, please print out its retrieval order according to the left-right direction; otherwise, print "NO";
special attention items:
1. 1 <= basketball number, number of basketballs <= 200;
2. The numbers on the basketball are not repeated;
3. The LR in the output must be capitalized;
enter
4,5,6,7,0,1,2
6,4,0,1,2,5,7
Output
RLRRRLL
Note: The order of taking out the basketball is "right, left, right, right, right, left, left"
enter
4,5,6,7,0,1,2
6,0,5,1,2,4,7
Output:
NO
Description: Unable to retrieve the basketball of the corresponding sequence
enter
1,2,3,4
1,2,3,5
Output
NO
Note: There is no basketball numbered 5, so the corresponding number data cannot be retrieved.
C++ source code:
- #include <iostream>
- #include <sstream>
- #include <queue>
- #include <string>
- #include <vector>
-
- using namespace std;
-
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
-
- queue<int> ball, get;
- deque<int> list;
- string input;
- getline(cin, input); // 获取第一行数据,篮球的放入顺序
- stringstream ss(input);
- string temp;
-
- while (getline(ss, temp, ',')) {
- ball.push(stoi(temp));
- }
-
- getline(cin, input); // 获取第二行数据,篮球的取出顺序
- ss.clear();
- ss.str(input);
- while (getline(ss, temp, ',')) {
- get.push(stoi(temp));
- }
-
- int ballNum = 0; // 统计输入的字符串中篮球编号的个数
- for (int i = 0; i < input.size(); i++)
- {
- if (input[i] != ',') {
- ballNum++;
- }
- }
-
- string direction; // 对篮球的取出顺序进行记录
- while (!get.empty()) {
- if (list.empty() && !ball.empty()) {
- list.push_back(ball.front());
- ball.pop();
- }
- if (list.empty()) {
- break;
- }
- int tempL = list.front(), tempR = list.back();
- int temp = get.front();
- if (temp == tempL) {
- get.pop();
- list.pop_front();
- direction = direction + 'L';
- }
- else if (temp == tempR) {
- get.pop();
- list.pop_back();
- direction = direction + 'R';
- }
- else {
- if (ball.empty()) {
- break;
- }
- list.push_back(ball.front());
- ball.pop();
- }
- }
-
- // 输出取出顺序
- if (ballNum == direction.size()) {
- cout << direction << endl;
- }
- else {
- cout << "NO" << endl;
- }
-
- system("pause");
- return 0;
- }