行编辑器
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 312(173 users) Total Accepted: 206(161 users) Rating: Special Judge: No
Description
这次我们要写一个简单的行编辑器,当按下‘#’时代表按下了一次退格符,当按下‘@’时代表一个退行符(使当前行的字符全部无效)。例如,假设从终端接收了这样的两行字符:
Whil#lr#e(s#*s)
outcha@putchar(*s=#++)
则实际有效的是下列两行:
While(*s)
putchar(*s++)
请你编写一个程序,输出实际有效的字符串。
Input
第一行是一个整数T,表示测试数据组数。
接下来每行为一个字符串(不含空格和任何空白),表示输入的原始字符串
Output
输出最终的正确字符串。
Sample Input
2
Whil#lr#e(s#*s)
outcha@putchar(*s=#++)
Sample Output
While(*s)
putchar(*s++)
Source
2016级新生程序设计全国邀请赛
题解
栈的简单应用,基本思路,将遇到的所有字符装到栈里(当然,除了‘@’和‘#’),如果遇到‘@’字符则清空整个栈,遇到‘#’则将栈顶元素弹出。此次使用了STL的栈来模拟这个过程,最后用字符数组存取这整个栈的元素,倒序输出,如果用数组模拟栈再来进行这个过程还可以省掉之前那个数组,过程也更简单些。代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using namespace std ;
void clear_( stack<char> &sta_ ){
while( !sta_.empty() ){
sta_.pop() ;
}
return ;
}
int main(){
int t ;
cin >> t ;
getchar() ;
while ( t -- ){
stack<char> sta_ ;
char ch = getchar() ;
while ( ch != '\n' ){
if ( ch == '#' ){
sta_.pop() ;
}else if ( ch == '@' ){
clear_(sta_) ;
}else{
sta_.push(ch) ;
}
ch = getchar() ;
}
char str[5005] ;
int index = 0 ;
while ( !sta_.empty() ){
str[index++] = sta_.top() ;
sta_.pop() ;
}
for ( int i = index - 1 ; i >= 0 ; i -- ){
cout << str[i] ;
}
cout << endl ;
}
return 0 ;
}