-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz-function-of-gray-string.go
81 lines (75 loc) · 1.04 KB
/
z-function-of-gray-string.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var T int
for fmt.Fscan(in, &T); T > 0; T-- {
var k, j int
fmt.Fscan(in, &k, &j)
if j == 0 {
fmt.Fprintln(out, 0)
continue
}
length := Length(k) - 1
var l, dest int
for l = 0; l <= 26; l++ {
dest += Length(l - 1)
letter := LetterNum(k, length, j+dest)
if letter != l+1 {
break
}
}
fmt.Fprintln(out, Length(l)-1)
}
}
func Length(k int) int {
return int(math.Pow(2, float64(k)))
}
func LetterNum(k, n, i int) int {
letter := k
l, r := 0, n-1
for l <= r {
mid := l + (r-l)>>1
if mid == i {
return letter
}
if mid > i {
r = mid - 1
} else {
l = mid + 1
}
letter--
}
return letter
}
//abacaba d abacaba e abac[A]ba d abacaba F abacaba d [A]bacaba e abacaba d abacaba GGG abacaba d abacaba e abacaba d abacaba F abacaba d abacaba e abacaba d abacaba
/*
10
25 20
25 19
25 18
25 17
25 16
25 15
25 14
25 13
25 12
25 11
3
0
1
0
15
0
1
0
3
0
*/