Guest User

Untitled

a guest
Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. ()[]{}<> : 0
  2. ([{<>}]) : 0
  3. <>{[]}() : 0
  4. {<>([])} : 0
  5. <(>)[{}] : 1
  6. <[({)}]> : 1
  7. [{<}]>() : 2
  8. {<>([}]) : 2
  9. <{(>})[] : 3
  10. [(]<){>} : 3
  11. <([>{)}] : 4
  12. (<{[>})] : 4
  13. (<[{)>}] : 5
  14. <{[(>})] : 5
  15. [{<(]}>) : 6
  16. (<{[)>}] : 6
  17.  
  18. l7~f&_f{/~;&}s,2/
  19.  
  20. l read a line of input
  21. 7~f& clear the lowest 3 bits of each character
  22. the goal is to convert brackets of the same type to the same char
  23. _ duplicate the resulting string, let's call it S
  24. f{…} for each character in S, and S (the char and S are pushed every time)
  25. swap the character with S
  26. / split S around that character, resulting in 3 pieces:
  27. before, between, after
  28. ~ dump the pieces on the stack
  29. ; pop the last piece
  30. & intersect the first 2 pieces
  31. after the loop, we have an array of strings
  32. containing the chars interlocking to the left with each char of S
  33. s join all the string into one string
  34. , get the string length
  35. 2/ divide by 2, because S has duplicated characters
  36.  
  37. y/([{</)]}>/
  38. s/.*/t& & & & /
  39. :b
  40. y/)]}>/]}>)/
  41. s/S*>(S*)>S* /1t/
  42. t
  43. s/S* //
  44. :
  45. s/(tS*)(S)(S*)2(S*t)/134/
  46. t
  47. s/S *$/&/
  48. tb
  49. s/s//g
  50. s/../1/g
  51.  
  52. perl -pe 'y/)]}>/([{</;for$x(/./g){$h{$x="\$x"}++&&s!$x(.*)$x!$z+=length$1,$1!e}$_=$z'
  53.  
  54. JmC/CdTzlsm@FPcsJd{J
  55.  
  56. (T`)]>}`([<{
  57. (D)(.*)1(.*)
  58. n$2n$3
  59. (?=(D).*n.*1)
  60. 1
  61. n
  62. <empty>
  63.  
  64. (T`)]>}`([<{
  65. (D)(.*)1(.*)
  66. #$2#$3
  67. (?=(D)[^#]*#[^#]*1)
  68. 1
  69. #
  70. <empty>
  71.  
  72. T`)]>}`([<{
  73.  
  74. (D)(.*)1(.*)
  75. n$2n$3
  76.  
  77. (?=(D).*n.*1)
  78. 1
  79.  
  80. n
  81. <empty>
  82.  
  83. x=>(a=b=0,[for(c of x)for(d of'1234')(e=c.charCodeAt()/26|0)==d?a^=1<<d:b^=(a>>d&1)<<d*4+e],f=y=>y&&y%2+f(y>>1))(b)/2
  84.  
  85. () => 40,41
  86. <> => 60,62
  87. [] => 91,93
  88. {} => 123,125
  89.  
  90. b^=(a>>d&1)<<d*4+e
  91.  
  92. f=y=>y&&y%2+f(y>>1)
  93.  
  94. f(y) => y && y%2 + f(y>>1)
  95. f(0b1001101) => 1 + f(0b100110) = 4
  96. f(0b100110) => 0 + f(0b10011) = 3
  97. f(0b10011) => 1 + f(0b1001) = 3
  98. f(0b1001) => 1 + f(0b100) = 2
  99. f(0b100) => 0 + f(0b10) = 1
  100. f(0b10) => 0 + f(0b1) = 1
  101. f(0b1) => 1 + f(0b0) = 1
  102. f(0b0) => 0 = 0
  103.  
  104. WITH v AS(SELECT b,MIN(p)i,MAX(p)a FROM(SELECT SUBSTR(TRANSLATE(:1,'])>}','[(<{'),LEVEL,1)b,LEVEL p FROM DUAL CONNECT BY LEVEL<9)GROUP BY b)SELECT COUNT(*)FROM v x,v y WHERE x.i<y.i AND x.a<y.a AND y.i<x.a;
  105.  
  106. WITH v AS( -- Compute min and max pos for each bracket type
  107. SELECT b,MIN(p)i,MAX(p)a
  108. FROM ( -- replace ending brackets by opening brakets and split the string
  109. SELECT SUBSTR(TRANSLATE(:1,'])>}','[(<{'),LEVEL,1)b,LEVEL p
  110. FROM DUAL
  111. CONNECT BY LEVEL<9
  112. )
  113. GROUP BY b
  114. )
  115. SELECT COUNT(*)
  116. FROM v x,v y
  117. WHERE x.i<y.i AND x.a<y.a AND y.i<x.a -- Apply restrictions for interlocking brackets
Add Comment
Please, Sign In to add comment