题目大意:
模拟字符串括号匹配 ,2(3)表示 2 是 3 的上司,国王没有上司,问你每个士兵的上司,如果是国王就输出0
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
#define dbg(x) cout << #x << '=' << x << endl
#define FA ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 10, M = 2 * N, mod = 1e9 + 7;
const double eps = 1e-8;
typedef long long LL;
typedef pair<int, int> PII;
int n, m;
int a[N];
stack<int> st;
void solve() {
cin >> n;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (isdigit(s[i])) {
//转数字
int j = i;
while (isdigit(s[j])) j++;
string t = s.substr(i, j - i + 1);
int sum = stoi(t);
i = j - 1;
if (st.size()) a[sum] = st.top();
st.push(sum);
} else if (s[i] == ')')
st.pop();
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
signed main() {
int T = 1;
// FA;
while (T--) {
solve();
}
}