程序员输入问题
描述
程序员输入程序出现差错时,可以采取以下的补救措施:按错了一个键时,可以补按一个退格符“#”,以表示前一个字符无效;发现当前一行有错,可以按一个退行符“@”,以表示“@”与前一个换行符之间的字符全部无效。
输入
输入一行字符,个数不超过 100。
输出
输出一行字符,表示实际有效字符。
输入样例 1
sdfosif@for(ii#=1,#;i<.#=8;i+++#);
输出样例 1
for(i=1;i<=8;i++);
#include <iostream>
#include <stack>
using namespace std;
int main() {
string input;
getline(cin, input);
stack<char> edits;
for (char ch : input) {
if (ch == '#') {
if (!edits.empty()) {
edits.pop();
}
} else if (ch == '@') {
while (!edits.empty()) {
edits.pop();
}
} else {
edits.push(ch);
}
}
string output;
while (!edits.empty()) {
output = edits.top() + output;
edits.pop();
}
cout << output << endl;
return 0;
}