Given a word w, rearrange the letters of w to construct another word s in such a way that s is lexicographically greater than w. In case of multiple answers, find the lexicographically smallest one.
5
ab
bb
hefg
dhck
dkhc
Output:
ba
no answer
hegf
dhkc
hcdk
C++ Implementation:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#define MAX 1000000
using namespace std;
int main() {
int n;
char s[MAX];
cin>>n;
while(n--)
{
cin>>s;
int len;
len=strlen(s);
if(next_permutation(s,s+len))
cout<<s<<endl;
else
cout<<"no answer"<<endl;
}
return 0;
}