-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (45 loc) · 886 Bytes
/
main.go
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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
func findSmallestLargerSeries(codes []string) string {
var output []string
for _, code := range codes {
str := []byte(code)
i := len(str) - 2
for i >= 0 && str[i] >= str[i+1] {
i--
}
if i == -1 {
output = append(output, "no answer")
continue
}
j := len(str) - 1
for str[j] <= str[i] {
j--
}
str[i], str[j] = str[j], str[i]
sort.Slice(str[i+1:], func(x, y int) bool {
return str[i+1:][x] < str[i+1:][y]
})
output = append(output, string(str))
}
return strings.Join(output, "\n")
}
func main() {
var n int
var codes []string
fmt.Scanln(&n)
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
for i := 0; i < n; i++ {
scanner.Scan()
code := scanner.Text()
codes = append(codes, code)
}
fmt.Println(findSmallestLargerSeries(codes))
}