本帖最后由 slore 于 2017-9-14 17:41 编辑
- def type(c)
- return :num if c =~ /[0-9]/
- return :lit if c =~ /[a-z]/
- end
-
- def is_expand_pattern(h, t)
- return false if !type(h) || type(h) != type(t)
- true
- end
-
- def expand(h, t, opt)
- len = t.ord - h.ord
- return '-' if len < 1 # b-a, l-a, z-z, 9-3, 7-6, 5-5
- return '' if len == 1 # i-j, 8-9
-
- if opt[0] == 3
- return '*' * (len - 1) * opt[1] #times
- end
-
- ex = []
- (h..t).each { |c|
- next if c == h || c == t
- ex.push c * opt[1] #times
- }
- ex.reverse! if opt[2] == 2 #reverse
- ex = ex.join('')
- return ex.upcase if opt[0] == 2 #upcase
- return ex
- end
-
- def main(opt, str, expected)
- i = 0;out = '';used = -1;
- str.each_char { |c|
- if c == '-' && used < i - 1 && is_expand_pattern(str[i-1], str[i+1])
- out << expand(str[i-1], str[i+1], opt)
- used = i + 1
- else
- out << c
- end
- i+=1
- }
- puts "#{expected}\n#{out}"
- end
-
- main([1,2,1], "abcs-w1234-9s-4zz", "abcsttuuvvw1234556677889s-4zz")
- main([2,3,2], "a-d-d", "aCCCBBBd-d")
- main([3,4,2], "di-jkstra2-6", "dijkstra2************6")
- main([1,2,1], "d-cc-dl-ld-aa-d9-89-74-56-64-9:i-j-k:i-k-z:j-i-k-1-4-8:j-i-k-n7-3-2",
- "d-ccdl-ld-aabbccd9-89-7456-64556677889:ij-k:ijjk-n:j-i-k-122334-8:j-i-kllmmn7-3-2")
- main([2,2,1], "d-cc-dl-ld-aa-d9-89-74-56-64-9:i-j-k:i-k-n:j-i-k-1-4-8:j-i-k-n7-3-2",
- "")
- main([2,2,2], "d-cc-dl-ld-aa-d9-89-74-56-64-9:i-j-k:i-k-n:j-i-k-1-4-8:j-i-k-n7-3-2",
- "")
- main([3,2,1], "d-cc-dl-ld-aa-d9-89-74-56-64-9:i-j-k:i-k-n:j-i-k-1-4-8:j-i-k-n7-3-2",
- "")
复制代码
abcsttuuvvw1234556677889s-4zz
abcsttuuvvw1234556677889s-4zz
aCCCBBBd-d
aCCCBBBd-d
dijkstra2************6
dijkstra2************6
d-ccdl-ld-aabbccd9-89-7456-64556677889:ij-k:ijjk-n:j-i-k-122334-8:j-i-kllmmn7-3-2
d-ccdl-ld-aabbccd9-89-7456-64556677889:ij-k:ijjk-z:j-i-k-122334-8:j-i-kllmmn7-3-2
d-ccdl-ld-aaBBCCd9-89-7456-64556677889:ij-k:iJJk-n:j-i-k-122334-8:j-i-kLLMMn7-3-2
d-ccdl-ld-aaCCBBd9-89-7456-64887766559:ij-k:iJJk-n:j-i-k-133224-8:j-i-kMMLLn7-3-2
d-ccdl-ld-aa****d9-89-7456-64********9:ij-k:i**k-n:j-i-k-1****4-8:j-i-k****n7-3-2
有一条规则没说清,所以有点出入。
就是是贪婪方式么?
i-k-n
i-k展开,k虽然用了,k-n还展开?
还是说,i-k展开,-n单独不成对,原样输出。
【输入输出样例2】给的a-d-d,但是d-d相同,所以还是输出-d无法判明。 |