# 损坏的打字机## 题目描述小C有一台打字机使用打字机打字时每在键盘上按一个字母键屏幕中光标后的位置就会出现对应的字母光标也会后移一位一开始只有一个光标没有字母。除此以外打字机还有三个按键分别是光标左移、光标右移以及删除按键功能为删除光标前的字符并将光标左移一位。如果光标已经位于最左侧则按下光标左移或删除按键时没有反应。如果光标已经位于最右侧则按下光标右移时没有反应。例如如果小C依次按下 a、b
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string repairTypewriter(string s) {
stack<char> st;
string result = "";
for (char c : s) {
if (c == '[') {
if (!st.empty()) {
result += st.top();
st.pop();
}
} else if (c == ']') {
if (!st.empty()) {
st.pop();
}
} else if (c == '}') {
if (!st.empty()) {
result += st.top();
st.pop();
}
result += '}';
} else {
st.push(c);
result += c;
}
}
return result;
}
int main() {
string s;
cin >> s;
string result = repairTypewriter(s);
cout << result << endl;
return 0;
}
``
原文地址: http://www.cveoy.top/t/topic/iWyO 著作权归作者所有。请勿转载和采集!