Files
python/TangDou/XiTi/1392.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

58 lines
1.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int main() {
//输入+输出重定向
freopen("../1392.in", "r", stdin);
freopen("../1392.out", "w", stdout);
int n;
stack<int> st;
int source[101] = {0};
int target[101] = {0};
unordered_map<int, int> _stackMap;
unordered_map<int, int> _outMap;
cin >> n;
//源数组+目标数组
for (int i = 1; i <= n; ++i) {
//原来的数组
// ----> 1 2 3 4
source[i] = i;
// ---->3 2 4 1
cin >> target[i];
}
//使用A进栈B出栈进行模拟
for (int i = 1; i <= n;) {
int targetNum = target[i];
//如果栈中没有这个数字,那么需要从源数组逐个增加进来
if (_stackMap.count(targetNum) == 0) {
for (int j = 1; j <= n; ++j) {
if (_stackMap.count(source[j]) == 0 && _outMap[source[j]] == 0) {
st.push(source[j]);
_stackMap[source[j]]++;
cout << "A";
if (source[j] == targetNum) {
break;
}
}
}
} else { //如果在栈里面有这个数字,那么需要逐个弹出,直接把这个数字弹出完毕
int c = st.top();
do {
st.pop();
_stackMap[c]--;
_outMap[c]++;
cout << "B";
i++;
} while (c != targetNum);
}
}
//关闭文件
fclose(stdin);
fclose(stdout);
return 0;
}