#C3669. Validate Operation Sequence

    ID: 47121 Type: Default 1000ms 256MiB

Validate Operation Sequence

Validate Operation Sequence

You are given a sequence of operations. Each operation is one of the following strings: open, close, read, or write.

The operations must follow these rules:

  • An open operation starts a new context.
  • A close operation ends the most recent open context. The sequence is invalid if a close operation does not have a corresponding open before it.
  • The operations read and write can only appear when there is an active context (i.e. after an open and before its corresponding close).
  • At the end of the sequence, there should be no unclosed contexts (i.e. every open must be matched with a close).

Your task is to determine whether the given operation sequence is valid.

The expected output is True if the sequence is valid and False otherwise.

Note: The input will be provided via standard input (stdin) and the output should be written to standard output (stdout). All formulas are represented in LaTeX. For example, the condition for a valid sequence at the end can be expressed as \(\text{depth} = 0\).

inputFormat

The first line contains a single integer \(n\) representing the number of operations. Each of the following \(n\) lines contains one operation which is one of the strings: open, close, read, or write.

For example:

4
open
read
write
close

outputFormat

Output a single line: True if the sequence of operations is valid according to the rules described, or False otherwise.

## sample
4
open
read
write
close
True