In the wizarding world of security, there are two kinds of researcher:
the idealistarranging hatand the mercenaryderanging hat.As we learned
last year, anarranging hatcarefully sorts out any list of letters
given to it intoascending order. However, aderanging hatperforms the
exact opposite function: putting asorted string of letters back into
its original order.The tool of choice for today’s discerning headwear
is a sorting network: a sequence of instruc-tions represented by a
list of pairs of numbersAiandBi, meaning that if at stepitheA-th
itemin the string is not already smaller than theB-th item, they
should be swapped immediately.Given a specific wordW, output a sorting
network that the deranging hat can use to form theword from its
original sorted letters.InputOne line containing one string of
lowercase Latin letters (‘a’-‘z’),S, containing at
most1000characters.OutputOutput at most10000lines, each containing two integers Ai and Bi(1≤Ai, Bi≤|S|) giving the i-th operation to perform.
Sample Input 1😊
bab
Sample Output 1
2 1
Sample Input 2
dude
Sample Output
4 3
3 2
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <math.h>
#include <bitset>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include<functional>
#include<time.h>
#include<stdlib.h>
#include<time.h>
#include <climits>
using namespace std;
#define MEM(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define EXP 1e-8
#define lowbit(x) (x&-x)
#define debug(x) cout<<x<<"#";
const int maxn =200907;
struct node {
char s;
int pos;
}a[maxn];
int main()
{
char s1[maxn];
cin >> s1;
int len = strlen(s1);
for (int i = 0; i < len; i++)
{
a[i].s = s1[i];
a[i].pos = i;
}
sort(s1, s1 + len);
for (int i = 0; i < len; i++)
{
if (s1[i] != a[i].s)
{
for (int j = i + 1; j < len; j++)
{
if (s1[j] == a[i].s)
{
if(s1[j]>=s1[i])
cout << j+1 << ' ' << i+1 << endl;
else cout << i+1 << ' ' << j+1 << endl;
s1[j] = s1[i];
break;
}
}
}
}
/*for(int i=0;i<4;i++)
cout << a[i].s << endl;*/
return 0;
}