-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBiggerIsGreater.cpp
More file actions
112 lines (81 loc) · 2.12 KB
/
BiggerIsGreater.cpp
File metadata and controls
112 lines (81 loc) · 2.12 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// problem: "https://www.hackerrank.com/challenges/bigger-is-greater/problem"
#include<iostream>
#include<math.h>
#include<string.h>
#include<ctype.h>
using namespace std;
int main()
{
long int i,j,k,t,n,l,num;
char str[100],a[100],b[100],min,temp;
cin>>t;
for(i=0;i<t;i++)
{
cin>>str;
l=strlen(str);
n=-1;
for(j=l-2;j>=0;j--)
{
if(str[j]<str[j+1])
{
n=j;
break;
}
}
if(n==-1)
{
cout<<"no answer"<<endl;
continue;
}
if((n==0)&&(str[0]>str[1]))
{
for(j=l-2;j>=0;j--)
{
if(str[j]!=str[j+1])
num=j;
}
temp=str[num];
str[num]=str[num+1];
str[num+1]=temp;
for(j=0;j<l;j++)
cout<<str[j];
cout<<endl;
continue;
}
min='}';
for(j=n+1;j<l;j++)
if((str[j]>str[n])&&(str[j]<min))
{
min=str[j];
num=j;
}
temp=str[n];
str[n]=str[num];
str[num]=temp;
for(j=0;j<=n;j++)
{
a[j]=str[j];
}
for(j=0;j<l-n-1;j++)
{
min='z';
for(k=n+1;k<l;k++)
if(isalpha(str[k]))
if(min>=str[k])
{
min=str[k];
num=k;
}
b[j]=min;
str[num]='_';
}
for(j=0;j<l-n-1;j++)
{
a[n+j+1]=b[j];
}
for(j=0;j<l;j++)
cout<<a[j];
cout<<endl;
}
return 0;
}