Submit  All submissions  Best solutions  Back to list 
NEXTPERM  Next permutation 
Wersja polska  English version 
Given string n, your task is to print next permutation of string n.
Input
The first line of the standard input contains one integer t (t<101) which is the number of test cases.
In each of the next t lines there is one integer l (l<20) which is length of string n. Line below you will be given string n.
Output
For each string n, print the next permutation of string n or ERROR if there is no such a permutation.
Example
Input:
2
3
acb
3
cba
Output:
bac
ERROR
Added by:  Piotr Ką kol 
Date:  20091222 
Time limit:  4.900s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: SCM qobi 
Resource:  Permutation 
hide comments
20131103 15:18:29 Piotr KÄ…kol
Thanks for the info! I'll fix it along with LOOK_SAY. Edit: Fixed and rejudged. Last edit: 20131207 00:20:24 

20131102 22:20:09 Mitch Schwartz
Test cases are weak; my submission 10406350 doesn't handle cases like cfedba properly but still passed. Last edit: 20131102 23:03:16 

20120901 19:12:04 Piotr KÄ…kol
@Avinash  Your answer for: 18
is: bdcofgpqarmhneljki .


20120901 13:11:07 AVINASH JAIN
Its giving wrong answer again and again. My program is running for the test cases given and for some random test cases. Please give some more test cases. Can n be zero(0). 

20100219 22:50:13 Piotr KÄ…kol
1) length < 20 2) Yes. For example succesive permutations of length 3 are: abc acb bac bca cab cba Then You should print ERROR. 3) Yes, only lowercase (but ERROR is made of uppercase letters). 4) No, they can't. 5) Good luck. ;) 

20100219 14:39:59 Alessandro Ferreira  UFMS
What's the limit for 'l'? My task is to find the next permutation in lexicographic ordering ? Does the string contain only lowercase letters? May appear repeated elements in string? Last edit: 20100219 18:33:11 