-
Notifications
You must be signed in to change notification settings - Fork 0
/
week4-7.c
48 lines (45 loc) · 885 Bytes
/
week4-7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int next_perm(char str[],int size){
int i=size-1;
int temp;
while(i>0&&str[i-1]>=str[i]){
i--;
}
if(i==0)
return 0;
int j = size - 1;
while (str[j] <= str[i - 1])
j--;
temp = str[i - 1];
str[i - 1] = str[j];
str[j] = temp;
// Reverse suffix
j = size - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
return 1;
}
int main()
{
int n,a;
scanf("%d",&n);
for(a=0;a<n;a++){
char str[100];
scanf("%s",str);
int check=next_perm(str,strlen(str));
if(check==1){
printf("%s",str);
}
else{
printf("game terminated");
}
}
return 0;
}