-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path10460.go
45 lines (39 loc) · 822 Bytes
/
10460.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
// UVa 10460 - Find the Permuted String
package main
import (
"fmt"
"os"
)
func insert(slice []byte, pos int, value byte) []byte {
slice = slice[0 : len(slice)+1]
copy(slice[pos+1:], slice[pos:])
slice[pos] = value
return slice
}
func solve(str string, n int) string {
n--
l := len(str)
indices := make([]int, l)
for i := l; i > 0; i-- {
indices[i-1] = n % i
n /= i
}
permuted := make([]byte, 0, l)
for i, idx := range indices {
permuted = insert(permuted, idx, str[i])
}
return string(permuted)
}
func main() {
in, _ := os.Open("10460.in")
defer in.Close()
out, _ := os.Create("10460.out")
defer out.Close()
var kase, n int
var str string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%s", &str)
fmt.Fscanf(in, "%d", &n)
fmt.Fprintln(out, solve(str, n))
}
}