给定一个字符串数组Q[] ,由以下类型的查询组成:
- “WRITE X”:在文档中写入一个字符X。
- “UNDO”:删除对文档所做的最后更改。
- “REDO”:恢复最近对文档执行的UNDO操作。
- “READ”:读取并打印文件的内容。
例子:
Input: Q = {“WRITE A”, “WRITE B”, “WRITE C”, “UNDO”, “READ”, “REDO”, “READ”}
Output: AB ABC
Explanation:
Perform “WRITE A” on the document. Therefore, the document contains only “A”.
Perform “WRITE B” on the document. Therefore, the document contains “AB”.
Perform “WRITE C” on the document. Therefore, the document contains “ABC”.
Perform “UNDO” on the document. Therefore, the document contains “AB”.
Print the contents of the document, i.e. “AB”
Perform “REDO” on the document. Therefore, the document contains “ABC”.
Print the contents of the document, i.e. “ABC”
Input: Q = {“WRITE x”, “WRITE y”, “UNDO”, “WRITE z”, “READ”, “REDO”, “READ”}
Output:xz xzy
方法:使用Stack可以解决这个问题。请按照以下步骤解决问题:
- 初始化两个堆栈,比如Undo和Redo 。
- 遍历字符串数组Q ,并执行以下操作:
- 如果遇到“WRITE”字符串,则将字符推送到撤消堆栈
- 如果遇到“UNDO”字符串,则从Undo堆栈中弹出顶部元素并将其推送到Redo堆栈。
- 如果遇到“REDO”字符串,则弹出重做堆栈的顶部元素并将其压入撤消堆栈。
- 如果遇到“READ”字符串,则以相反的顺序打印Undo堆栈的所有元素。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to perform
// "WRITE X" operation
void WRITE(stack& Undo,
char X)
{
// Push an element to
// the top of stack
Undo.push(X);
}
// Function to perform
// "UNDO" operation
void UNDO(stack& Undo,
stack& Redo)
{
// Stores top element
// of the stack
char X = Undo.top();
// Erase top element
// of the stack
Undo.pop();
// Push an element to
// the top of stack
Redo.push(X);
}
// Function to perform
// "REDO" operation
void REDO(stack& Undo,
stack& Redo)
{
// Stores the top element
// of the stack
char X = Redo.top();
// Erase the top element
// of the stack
Redo.pop();
// Push an element to
// the top of the stack
Undo.push(X);
}
// Function to perform
// "READ" operation
void READ(stack Undo)
{
// Store elements of stack
// in reverse order
stack revOrder;
// Traverse Undo stack
while (!Undo.empty()) {
// Push an element to
// the top of stack
revOrder.push(Undo.top());
// Erase top element
// of stack
Undo.pop();
}
while (!revOrder.empty()) {
// Print the top element
// of the stack
cout << revOrder.top();
Undo.push(revOrder.top());
// Erase the top element
// of the stack
revOrder.pop();
}
cout << " ";
}
// Function to perform the
// queries on the document
void QUERY(vector Q)
{
// Stores the history of all
// the queries that have been
// processed on the document
stack Undo;
// Stores the elements
// of REDO query
stack Redo;
// Stores total count
// of queries
int N = Q.size();
// Traverse all the query
for (int i = 0; i < N; i++) {
if (Q[i] == "UNDO") {
UNDO(Undo, Redo);
}
else if (Q[i] == "REDO") {
REDO(Undo, Redo);
}
else if (Q[i] == "READ") {
READ(Undo);
}
else {
WRITE(Undo, Q[i][6]);
}
}
}
// Driver Code
int main()
{
vector Q = { "WRITE A", "WRITE B",
"WRITE C", "UNDO",
"READ", "REDO", "READ" };
QUERY(Q);
return 0;
}
Python3
# Python Program to implement
# the above approach
global Undo
global Redo
# Stores the history of all
# the queries that have been
# processed on the document
Undo = []
# Stores the elements
# of REDO query
Redo = []
# Function to perform
# "WRITE X" operation
def WRITE(Undo, X):
# Push an element to
# the top of stack
Undo.append(X)
# Function to perform
# "UNDO" operation
def UNDO(Undo, Redo):
# Stores top element
# of the stack
X = Undo[-1]
# Erase top element
# of the stack
Undo.pop()
# Push an element to
# the top of stack
Redo.append(X)
# Function to perform
# "REDO" operation
def REDO(Undo, Redo):
# Stores the top element
# of the stack
X = Redo[-1]
# Erase the top element
# of the stack
Redo.pop()
# Push an element to
# the top of the stack
Undo.append(X)
# Function to perform
# "READ" operation
def READ(Undo):
print(*Undo, sep = "",
end = " ")
# Function to perform the
# queries on the document
def QUERY(Q):
# Stores total count
# of queries
N = len(Q)
# Traverse all the query
for i in range(N):
if(Q[i] == "UNDO"):
UNDO(Undo, Redo)
elif(Q[i] == "REDO"):
REDO(Undo, Redo)
elif(Q[i] == "READ"):
READ(Undo)
else:
WRITE(Undo, Q[i][6])
# Driver Code
Q = ["WRITE A", "WRITE B", "WRITE C",
"UNDO", "READ", "REDO", "READ"]
QUERY(Q)
#This code is contributed by avanitrachhadiya2155
AB ABC
时间复杂度:
撤销: O(1)
重做: O(1)
写: O(1)
READ: (N),其中N表示Undo栈的大小
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live