<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

397. Text Editor
Time limit per test: 1.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



The simplest text editor "Open Word" allows to create and edit only one word. The editor processes keys 'a' -- 'z', and also 'L' (to the left) and 'R' (to the right). After starting his work the editor immediately creates an empty word and sets its cursor to the left-most position. When one of keys 'a' -- 'z' is pressed, the text editor inserts corresponding symbol just after the cursor. After that a cursor moves one position to the right in such a way that it is placed just after new symbol. When key 'L' or 'R' is pressed, the cursor moves one position to the left or to the right respectively. If the cursor can't be moved because it is placed at the left-most or right-most position the command is ignored. Developers of "Open Word" didn't think about the effectiveness so the editor is working slowly if a lot of keys have been pressed.

Your task is to write a program that can process a sequence of key pressings emulating this editor and output result string.

Input
The input file contains one string which consists of symbols 'a' -- 'z', 'L' and 'R'. The string length is not less than 1 and doesn't exceed 106.

Output
Write a required string to the output file.

Example(s)
sample input
sample output
abLcd
acdb

sample input
sample output
icpLLLLLacmRRRRRRRRRRRRc
acmicpc

<|response|>
1. Abridged Problem Statement  
You start with an empty word and a cursor at position 0. You receive a sequence of key presses, each being:  
- a lowercase letter ‘a’–‘z’: insert that letter immediately after the cursor, then move the cursor right by one.  
- ‘L’: move the cursor left by one (if not already at the beginning).  
- ‘R’: move the cursor right by one (if not already at the end).  
After processing all key presses, output the final word.

2. Key Observations  
- Directly inserting into or erasing from the middle of a dynamic array/string is O(N) per operation in the worst case, leading to O(N²) overall for up to 10^6 operations.  
- We can avoid expensive middle inserts by maintaining two stacks (or deques):  
  • `left` stores the characters to the left of the cursor (in normal order).  
  • `right` stores the characters to the right of the cursor (in normal order).  
- Operations translate to O(1) stack operations:  
  • Insert letter: push onto `left`.  
  • ‘L’: if `left` not empty, pop from `left` and push onto `right`.  
  • ‘R’: if `right` not empty, pop from `right` and push onto `left`.  
- At the end, the full text is the contents of `left` followed by the contents of `right` in reverse order.

3. Full Solution Approach  
- Read the input string of operations.  
- Initialize two empty stacks (e.g. `vector<char>` or `deque<char>` in C++): `left`, `right`.  
- For each character `c` in the input:  
  • If `c` in ‘a’..‘z’: `left.push_back(c)`.  
  • Else if `c == 'L'` and `left` is not empty: move one character from `left` to `right`.  
  • Else if `c == 'R'` and `right` is not empty: move one character from `right` to `left`.  
- After processing, output all characters in `left` in order, then all characters in `right` in reverse order.  
- This runs in O(N) time and O(N) space, which easily handles N up to 10^6.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Read the sequence of operations (length up to 10^6).
    string ops;
    cin >> ops;

    // Two stacks/deques to simulate the text around the cursor.
    vector<char> left;    // characters to the left of the cursor
    vector<char> right;   // characters to the right of the cursor

    // Reserve space to avoid repeated reallocations
    left.reserve(ops.size());
    right.reserve(ops.size());

    // Process each operation in O(1) amortized time
    for (char c : ops) {
        if (c == 'L') {
            // Move cursor left: pop from left -> push onto right
            if (!left.empty()) {
                right.push_back(left.back());
                left.pop_back();
            }
        }
        else if (c == 'R') {
            // Move cursor right: pop from right -> push onto left
            if (!right.empty()) {
                left.push_back(right.back());
                right.pop_back();
            }
        }
        else {
            // Insert letter: push onto left (cursor moves right)
            left.push_back(c);
        }
    }

    // Output the final word.
    // First all of left (in order), then right (in reverse order).
    for (char c : left) {
        cout << c;
    }
    for (auto it = right.rbegin(); it != right.rend(); ++it) {
        cout << *it;
    }
    cout << '\n';

    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    # Read entire input (one line of operations)
    ops = sys.stdin.read().strip()

    # Two lists to act as stacks
    left = []   # characters to the left of the cursor
    right = []  # characters to the right of the cursor

    # Process each operation
    for c in ops:
        if c == 'L':
            # Move cursor left
            if left:
                right.append(left.pop())
        elif c == 'R':
            # Move cursor right
            if right:
                left.append(right.pop())
        else:
            # Insert a letter
            left.append(c)

    # Final text = left + reversed(right)
    # right is stored in normal order, so reverse it
    sys.stdout.write(''.join(left) + ''.join(reversed(right)))

if __name__ == "__main__":
    main()
```