题目
4. 程序输入问题
题目ID:6349必做题100分
最新提交:
Accepted
100 分
历史最高:
Accepted
100 分
时间限制: 1000ms
空间限制: 65536kB
题目描述
程序员输入程序,出现差错时可以采取以下补救措施:敲错了一个键时,可以补敲一个退格符“#”,表示前一个字符无效;发现当前一行有错,可以敲入一个退行符“@”,表示“@”与前一个换行符之间的字符全部无效。如果#或者@按多了,即前方没有可以消除的字符或字符串,则无视此操作。
输入格式
输入若干行字符串,包含@、#和其他可显示字符。
输出格式
输出这行字符串实际读入程序的内容。
样例
Input 1
he###llo##world print
@ xixi@
haha##
Output 1
lworld ha
数据范围
每行字符串长度<=100,最多100行,
思路
要是按‘@’
就删除整行,可以用while只要不为0就pop。
if (a[i] == '@') {
while (!t.empty()) {
t.pop();
}
}
要是按‘#’
就删以个符号,用pop,当然,栈不能空。
else if (a[i] == '#') {
if (!t.empty()) {
t.pop();
}
}
剩下的直接输入。
else {
t.push(a[i]);
}
倒序存到另一个栈里。
while (!t.empty()) {
y.push(t.top());
t.pop();
}
输出。
while (!y.empty()) {
cout << y.top();
y.pop();
}
cout << endl;
代码(伪)
#include <bits/stdc++.h>
#include <stack>
using namespace std;
stack<char> t;
stack<char> y;
int sum = 0, s = 0;
int main() {
string a;
while (cin >> a) {
for (int i = 0; i < a.size(); i++) {
if (a[i] == '@') {
循环;
}
else if (a[i] == '#') {
删除;
}
else {
输入;
}
}
while (!t.empty()) {
y.push(t.top());
t.pop();
}
while (!y.empty()) {
输出;
}
cout << endl;
}
return 0;
}