Under construction

Still being built. Things may change

Deranging Hat

Interview

Time limit2sMemory limit512 MB

Summary
Given a string, find a sorting network that turns its sorted letters back into the original string, following a specified rule.
Level

Medium5 of 10

Topics
Simulation, Sorting, Greedy, Array
Solved
No attempts yet

Problem

A sorting network is a list of pairs of integers (A1,B1),(A2,B2),…(A_1, B_1), (A_2, B_2), \ldots. Applying a pair (A,B)(A, B) to a string follows a single rule. If the AA-th character is not smaller than the BB-th character, swap the two characters, and otherwise leave the string as it is. The pairs of the list are applied one after another, from the first pair to the last.

An arranging hat lays out the letters it is given in ascending order. A deranging hat runs the other way. It starts from those sorted letters and puts the original word back together.

You are given a string SS of lowercase letters. Find a sorting network that turns the sorted letters of SS back into SS.

Input

The first line contains a string SS of lowercase Latin letters, 'a' to 'z'. (1≤∣S∣≤10001 \le |S| \le 1000)

Output

Several sorting networks usually work. Only the list built by the following rule is accepted.

Let TT be the string SS with its characters sorted in ascending order. Set the string XX to SS and start with an empty list. For i=1,2,…,∣S∣i = 1, 2, \ldots, |S| in this order, do the following. If Xi=TiX_i = T_i, do nothing. Otherwise take the smallest jj with j>ij > i and Xj=TiX_j = T_i, append the pair (j,i)(j, i) to the end of the list, then swap XiX_i and XjX_j. Such a jj always exists.

Print the pairs of the list in the reverse of the order in which they were appended, one pair per line, the two integers separated by a single space. Print nothing if the list is empty. The list holds at most ∣S∣−1|S| - 1 pairs, so the output never exceeds 10000 lines, and every printed integer is between 11 and ∣S∣|S|. Applying this list to TT gives SS.

Examples2

  1. Example 1

    Input
    bab
    
    Expected output
    2 1
    
  2. Example 2

    Input
    dude
    
    Expected output
    4 3
    3 2