Sunflowers in the Gust

medium simulation strings two-pointers arrays 1000 ms · 256 MB

Sunflowers in the Gust

Marta grows n sunflowers in a single straight row, numbered 1 to n from left to right. A morning gust has just passed, and now every sunflower is in one of three states:

  • L: it leans to the left, toward flower i − 1;
  • R: it leans to the right, toward flower i + 1;
  • .: it stands upright.

The stems are weak and the heads are heavy, so leaning flowers push on their neighbours. The row changes in whole seconds. During each second, every upright flower i is looked at, all at the same time, using the states at the start of that second:

  • it is pushed from the left if flower i − 1 exists and leans R (into it);
  • it is pushed from the right if flower i + 1 exists and leans L (into it).

If an upright flower is pushed from exactly one side, it tips away from that side: pushed from the left it becomes R, pushed from the right it becomes L. If it is pushed from both sides in the same second, the two pushes cancel and it stays upright. An upright flower that is not pushed stays upright. A flower that leans never changes again; a flower that tipped during a second starts pushing its own neighbour in the next second. The ends of the row are open: the first flower has no left neighbour and the last flower has no right neighbour.

Eventually the row stops changing. Print what it looks like then.

Input

The first line contains n (1 ≤ n ≤ 10^6), the number of sunflowers.

The second line contains a string of exactly n characters, each of them L, R or .; the i-th character is the state of flower i right after the gust.

Output

Print a single line with n characters: the state of every flower, in order, once the row has stopped changing.

Notes

In the first example, flower 1 leans right and flower 5 leans left. After one second flowers 2 and 4 have tipped (toward flower 3); in the next second flower 3 is pushed from both sides at once and stays upright for good. Flower 6 is never pushed: flower 5 leans away from it and flower 7 leans away from it too. Flowers 8 and 9 sit between a right-leaner and a left-leaner with an even gap, so each tips toward its nearer pusher. Flowers 11 and 12 lie between an L and an R that lean away from them, and stay upright; flowers 14 and 15 are tipped one after another by flower 13.

In the second example, flower 1 is tipped left by flower 2, and the last flower is tipped right by flower 5.

Example 1

Input

15
R...L.R..L..R..

Output

RR.LL.RRLL..RRR

Example 2

Input

6
.L..R.

Output

LL..RR

Hints · reveal one at a time if you're stuck

Hint 1

Look at one upright flower and ask what decides its fate: which neighbours can push it, and what happens when pushes arrive from both sides in the same second?

Hint 2

A push moves through upright flowers only, one flower per second, and a leaning flower facing away from an upright one never pushes it. So consider each maximal run of upright flowers between two leaning ones: which pusher reaches each flower first, and what happens at a tie?

Hint 3

Each run can be settled on its own, so you do not need to simulate the seconds. Compute, for every upright flower, how far away the nearest right-pushing flower on its left and the nearest left-pushing flower on its right are, with the blocking rule taken into account, using one pass in each direction.

report an issue Prefer your own editor? sooks prep 29 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.