给定一个字符串数组Q [] ,它由以下类型的查询组成:
- “ WRITE X”:将字符X写入文档。
- “ UNDO”:删除对文档所做的最后更改。
- “重做”:恢复对文档执行的最新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”字符串,请从“撤消”堆栈中弹出顶部元素,然后将其推入“重做”堆栈。
- 如果遇到“ REDO”字符串,则弹出“ Redo”堆栈的顶部元素,并将其推入“ Undo”堆栈。
- 如果遇到“ READ”字符串,则以相反顺序打印撤消堆栈的所有元素。
下面是上述方法的实现:
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)
读取: (N),其中N表示撤消堆栈的大小
辅助空间: O(N)