Description
编写程序模拟word中的“重做Redo”“撤销Undo” 两个按钮。即键盘输入一段文字(不能含#,e.g., I as Tom whether he will go to Beijingh)之后输入“#U”(“U”代表Undo)则撤销最后一个输入的字符(“h”),在输出位置重新输出一遍新撤销后的串。这时可以再继续输 入。当然,也可以输入“#R”恢复刚才删除的输入h。每有一次#R都要输出一下新的结果。当然,可以同时连续输入多个#U或多个#R。比如,输 入#U#U#R#U#U#U#R#R。
Input
第一行为一个数字T,代表一共有T组测试样例,每组测试样例包含以下内容:
每行包含一串字母(包含空格,不超过100个字符)。
开始第一个字母不能为“#”,即第一个字母必须要敲入文字。
“#R”表示重做。
“#U”表示撤销。
“##”表示本次测试样例结束。
Output
每输入一次重做,都要输出操作之后的文字
Sample Input
1
Hello world !
#U
#U
#U
#R
Haha
#U
#U
#R
##
Sample Output
Hello world
Hello worldHah
HINT
考察知识点:双向链表, 时间复杂度O(n),空间复杂度O(n)
Append Code
析:可以用两个链表,一个用来模拟,一个用来存储撤回的字符。当然也可以用其他的STL,也可以用数组。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <deque>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
list<char> l, ll;
list<char> :: iterator it;
char s[100];
int main(){
int T; cin >> T;
getchar();
while(T--){
l.clear();
ll.clear();
while(gets(s)){
if(s[0] == '#' && s[1] == '#') break;
if(s[0] != '#'){
for(int i = 0; s[i]; ++i) l.push_back(s[i]);
}
else{
if(s[1] == 'U'){
it = l.end();
--it;
ll.push_front(*it);
l.erase(it);
}
else{
l.push_back(*ll.begin());
ll.erase(ll.begin());
for(it = l.begin(); it != l.end(); ++it)
printf("%c", *it);
printf("
");
}
}
}
}
return 0;
}