标签:
Description
TonyY 是一个 word 小白,今天他对 word 中撤销和恢复功能特别感兴趣,玩耍了一个上午(mdzz~) ,现在他知道了它们的功能和快捷键:撤销:ctrl+z,可以撤销最近 1 次之前的恢复和 input 操作。恢复:ctrl+y,可以恢复最近 1 次之前的撤销操作,但是 input 操作之前的撤销操作不能被恢复。当然,TonyY 还要往里写东西,操作格式为 input str(长度 m 1=<m<=30) 。现在他对 word 玩耍了起来,想知道玩耍完的结果,你可以帮助他吗?
Input
输入第一行为一个正整数 n(0<=n<=10000) ,表示 TonyY 的操作次数。接下来 n 行,为上述的操作之一。其中可能有不合法操作,直接跳过即可。
Output
输出 word 中的内容,字符串间用空格隔开,如果 word 为空,输出”No output”
Sample Input
4
input a
ctrl+z
ctrl+y
input b
5
input a
input b
ctrl+z
input d
ctrl+y
Sample Output
a b
a d
思路
一开始一直去模拟这个过程,写到最后自己都乱掉了,其实仔细思考一下就会发现其中逻辑很简单,撤销操作是对恢复与input生效的,那么只要输入保存字符串的栈不为空,那么我就可以实行撤销操作,因为保存字符串的栈中的字符串要么是input得到的,要么是恢复得到的。而对于恢复操作,只要判断一下保存撤销的字符串的栈是否为空就好了,不为空就能恢复。另外对于input之前的撤销操作不能恢复,因此,只要在每次输入的时候,清空保存撤销的字符串的栈就好了。
#include<iostream>
#include<string>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn = 10005;
int main()
{
int N,i;
stack<string>kstr,tmp;
string opt,str,a[maxn];
scanf("%d",&N);
while (N--)
{
cin >> opt;
if (opt[0] == ‘i‘)
{
cin >> str;
kstr.push(str);
while (!tmp.empty())
{
tmp.pop();
}
}
else if (opt[5] == ‘z‘)
{
if (!kstr.empty())
{
tmp.push(kstr.top());
kstr.pop();
}
}
else if (opt[5] == ‘y‘)
{
if (!tmp.empty())
{
kstr.push(tmp.top());
tmp.pop();
}
}
}
int cnt = 0;
while (!kstr.empty())
{
a[cnt++] = kstr.top();
kstr.pop();
}
if (!cnt)
{
printf("No output\n");
}
else
{
for (i = cnt - 1;i >= 0;i--)
{
i?cout << a[i] << " " :cout << a[i];
}
cout << endl;
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zzy19961112/p/5902249.html