-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bigger is Greater.cs
56 lines (55 loc) · 1.48 KB
/
Bigger is Greater.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static bool nextPermutation(char[] array)
{
// Find non‐increasing suffix
int i = array.Length - 1;
while (i > 0 && array[i - 1] >= array[i])
i--;
if (i <= 0)
return false;
// Find successor to pivot
int j = array.Length - 1;
while (array[j] <= array[i - 1])
j--;
char temp = array[i - 1];
array[i - 1] = array[j];
array[j] = temp;
// Reverse suffix
j = array.Length - 1;
while (i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
return true;
}
static void Main(string[] args)
{
int t = int.Parse(Console.ReadLine());
while (t-- > 0)
{
string w = Console.ReadLine();
char[] perm = w.ToCharArray();
bool res = nextPermutation(perm);
if (res)
{
Console.WriteLine(new string(perm));
}
else
{
Console.WriteLine("no answer");
}
}
}
}
}