Little Monkey Programming C++ | Orbit

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

In order to reorganize the carriages, you can use transfer station C. This is a station where any number of carriages can be parked, but because the end is blocked, the carriages entering C must exit C in the reverse order . For each carriage, once you enter C, you cannot return to A ; once you move from C to B , you cannot return to C. In other words , the carriage can only move from A to C or from C to B.

【enter】

The first line contains an integer  n .

The second line contains  n  space-separated strings  s 1, s 2,⋯, sn , ensuring that the string  si  only contains uppercase and lowercase letters.

【Output】

One line, containing  n  spaces separated integers, represents the result.

【Input sample】

5
ab a aa ba aa

【Output sample】

2 3 5 4 1

[Detailed code explanation]

#include <bits/stdc++.h>
#include <stack>
using namespace std;
int n;
stack<int> st;
string s[100010], mins[100010];
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) cin >> s[i];
    
    mins[n] = s[n];
    for (int i=n-1; i>=1; i--) {
        mins[i] = min(mins[i+1], s[i]);
    }

    for (int i=1; i<=n; i++) {
        st.push(i);
        while (st.size() && s[st.top()]<=mins[i+1]) {
            cout << st.top() << " ";
            st.pop();
        }
    }
    while (st.size()) {
        cout << st.top() << " ";
        st.pop();
    }
    return 0;
}

【operation result】

5
ab a aa ba aa
2 3 5 4 1

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133914755