-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathchapter02.rb
executable file
·129 lines (112 loc) · 3.38 KB
/
chapter02.rb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
# -*- coding: UTF-8 -*-
def compare_1(long_string, short_string)
short_string.each_char{|s|
return false unless long_string.include? s
}
true
end
def compare_2(long_string, short_string)
long_string = long_string.chars.sort.join
short_string = short_string.chars.sort.join
i,j, n, m = 0, 0, long_string.length, short_string.length
while i < n && j < m
i += 1 while long_string[i] < short_string[j] && i < n - 1
break unless long_string[i] == short_string[j]
j += 1
end
j == m
end
def count_sort(src)
dst = [""] * src.length
tmp = [0] * 26
src.each_char{| c| tmp[ c.ord - "A".ord ] += 1}
(1...(tmp.length)).each{ |i| tmp[i] += tmp[i - 1]}
(src.length - 1).downto(0){ |i|
c = src[i]
index = c.ord - "A".ord
dst[tmp[index] - 1] = c
tmp[index] -= 1
}
dst.join
end
def compare_3(long_string, short_string)
sorted_long = count_sort(long_string)
sorted_short = count_sort(short_string)
pos_long, pos_short = 0, 0
while pos_short < sorted_short.length && pos_long < sorted_long.length
while (sorted_long[pos_long] < sorted_short[pos_short] && pos_long < sorted_long.length - 1)
pos_long += 1
end
break if (sorted_long[pos_long] != sorted_short[pos_short])
pos_short += 1
end
pos_short == sorted_short.length
end
#这个方法依赖ruby 2.0以上版本, 原因在 String.ord是在ruby 2.0以上才有
#在2.0以前版本可以使用 'A'.unpack('C')[0] 来获得ASCII
def compare_4(long_string, short_string)
hash_temp , count = [0] * 26, 0
short_string.each_char{ |c|
index = c.ord - "A".ord
if hash_temp[ index ] == 0
hash_temp[index] += 1
count += 1
end
}
long_string.each_char{ |c|
index = c.ord - "A".ord
if(hash_temp[index] == 1)
hash_temp[index] -= 1
count -= 1
end
}
count == 0
end
#这个方法依赖ruby 2.0以上版本, 原因在 String.ord是在ruby 2.0以上才有
#在2.0以前版本可以使用 'A'.unpack('C')[0] 来获得ASCII
def compare_5(long_string, short_string)
hash_temp = [false] * 26
short_string.each_char{ |c|
hash_temp[ c.ord - "A".ord ] = true
}
long_string.each_char{ |c|
hash_temp[ c.ord - "A".ord ] = false
}
!hash_temp.include? true
end
#这个方法依赖ruby 2.0以上版本, 原因在 String.ord是在ruby 2.0以上才有
#在2.0以前版本可以使用 'A'.unpack('C')[0] 来获得ASCII
def compare_6(long_string, short_string)
prime_number = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
product = long_string.chars.inject(1) { |product, n| product * prime_number[ n.ord - "A".ord] }
short_string.each_char{|c|
return false unless product % prime_number[ c.ord - "A".ord] == 0
}
true
end
#这个是ruby的实现方式
def compare_ruby(long_string, short_string)
short_string.chars - long_string.chars == []
end
if __FILE__== $0
long = "ABCDEFGHLMNOPQRSDCGSRQPX"
short = "DCCCCGSRQPX"
p compare_1(long, short)
p compare_2(long, short)
p compare_3(long, short)
p compare_4(long, short)
p compare_5(long, short)
p compare_6(long, short)
p compare_ruby(long, short)
puts
long = "ABCDEFGHLMNOPQRS"
short = "DCGSRQPX"
p compare_1(long, short)
p compare_2(long, short)
p compare_3(long, short)
p compare_4(long, short)
p compare_5(long, short)
p compare_6(long, short)
p compare_ruby(long, short)
end