Let
there be two arrays of sizes $m$ and $n$ respectively. The array $M$
of size $m$ contains a combination of these symbols: $S = {'p', 'q',
'r', 's', 't'}$ and the other array $N$ of size $n$ contains an array
of numbers. These two arrays are read linearly. So to keep a track of
the number of elements read and interpreted in both the arrays, there
are two index variables $p_1$ and $p_2$ used. Each time an element in
$M$ is read, $p_1$ increments. Each time an element in $N$ is read or
manipulated, $p_2$ increments.
The
below describes the pattern to be found in the array $M$, and also
defines the rules of what must be done for each of the patterns
found. Write a program to implement these rules and ensure that cases
that can't be handled return with a proper error message.
Pattern
|
Rule
|
Pqrst
|
Swap
the first with the second, the second with the third, the third
with the fourth and the fourth with the fifth.
|
pqrs
|
This
time, swap with the fifth number
|
pqr
|
This
time, swap with the fourth number
|
pq
|
Swap
the first number (at $p_2$ in $N$)with the third number.
|
q
|
Increment
the number by one
|
r
|
Decrement
the number by one
|
s
|
Square
of the number is returned.
|
t
|
Cube
of the number is returned.
|
P
|
Swap
the first number (at the position $p_2$) with the second number
|
Note
that, the pattern in the above table is arranged in the descending
order of priority. That is, if pattern 'pqrst' is found, the
corresponding rule is applied and not the other rules because it is
of the highest priority.
Solve
this by writing a program in C++ or any other language you prefer.
(the
solution will be added shortly. In the meanwhile, be the first
one to post the answer in the comments)