算法与数据结构实验题 3.9 文档

实验任务

TonyY 是一个word 小白,今天他对 word 中撤销和恢复功能特别感兴趣,玩耍了一个上午(mdzz~),现在他知道了它们的功能和快捷键:

撤销:ctrl+z,可以撤销最近 1 次之前的恢复和 input 操作。

恢复:ctrl+y,可以恢复最近1 次之前的撤销操作,但是 input 操作之前的撤销操作不能被恢复。

当然,TonyY 还要往里写东西,操作格式为 input str(str中不含空格,str长度为m,1=<m<=30)。

现在他对 word 玩耍了起来,想知道玩耍完的结果,你可以帮助他吗?

数据输入

输入第一行为一个正整数 n(0<=n<=10000),表示 TonyY 的操作次数。

接下来 n 行,为上述的操作之一。

其中可能有不合法操作,直接跳过即可。

数据输出

输出 word 中的内容,字符串间用空格隔开,如果 word 为空,输出”No output”

输入示例

4
input a
ctrl+z
ctrl+y
input b

输出示例

a b

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=2e9+1;

int T=1;
string opt,s;
vector<string>s1,s2;

int main()
{
	ios::sync_with_stdio(0);
	
	cin>>T;
	while(T--)
	{
		cin>>opt;
		if(opt=="input") {
			s2.clear();
			cin>>s;
			s1.push_back(s);
		}
		if(opt=="ctrl+z") {
			if(!s1.empty()) {
				s2.push_back(s1.back());
				s1.pop_back();
			}
		}
		if(opt=="ctrl+y") {
			if(!s2.empty()) {
				s1.push_back(s2.back());
				s2.pop_back();
			}
		}
	}
	if(s1.empty()) cout<<"No output";
	else for(int i=0;i<s1.size();i++) cout<<s1[i]<<' ';
	return 0;
}

思路
没什么好说的,很纯的模拟
其中,栈s1作为文本主干,栈s2作为撤销字符的保存栈,两个栈根据功能交错利用即可,这里图输出方便选择使用vector来替代stack(论vector为什么是万能的)

posted @ 2024-11-25 22:41  Severj  阅读(111)  评论(0)    收藏  举报