Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. Testcase #1
  2.  
  3. Testcase #2
  4.  
  5. Testcase #3
  6.  
  7. Testcase #4
  8.  
  9. ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
  10.  
  11. ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
  12. ABCDEF
  13. CDEFAB
  14. B
  15. 2
  16. ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
  17. HELLO
  18. WORLD
  19. SUCKS
  20. 0
  21. ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
  22. ABCXD
  23. BCDEF
  24. CD
  25. 3
  26. ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
  27. X
  28. Y
  29. Z
  30. 0
  31.  
  32. s(A,[H|R]):-(A=[H|T],s(T,R);s(A,R)).
  33. s([],[]).
  34. r(A,B,C,O):-(r(A,B,C,0,O),!;O=0).
  35. r(A,B,C,M,O):-s(S,A),s(S,B),append([_,C,_],S),length(S,L),L>M,!,(r(A,B,C,L,O),!;L=O).
  36.  
  37. ?- r("abcxd","bcdef","cd",O).
  38. O = 3.
  39.  
  40. from itertools import*
  41. def f(a,b,c):
  42. f=lambda x:set(''.join(z)for y in range(len(x))for z in combinations(x,y))
  43. return max(len(x)for x in f(a).intersection(f(b))if c in x or not x)
  44.  
  45. k⊇ᵐ=h.s~t?∨0
  46.  
  47. k⊇ᵐ=h.s~t?∨0
  48. k Take the input, apart from the last element.
  49. ⊇ᵐ Find the longest subsequence of each element
  50. = such that those subsequences are equal
  51. h and that subsequence
  52. s has a substring
  53. ~t? that's the last element of the input.
  54. . If you can, return the subsequence.
  55. ∨0 If all else fails, return 0.
  56.  
  57. n/)`{?)}+{['']1/{`{1$+}+%}/}/&,{,}%0+$-1=
  58.  
  59. n/ # split the input at newline into three strings
  60. )` # take the last one and stringify it
  61. {?)}+ # and build a code block to filter for any string
  62. # which has that one as a substring (for later use)
  63. { # now loop over the remaining two strings and
  64. # build their subsequences
  65. [''] # start with the set of the empty string
  66. 1/{ # loop over each character of the string
  67. `{ # apply code block to each member of the set
  68. 1$+ # copy member and add the current character
  69. }+% # -> set is updated with latest character
  70. }/
  71. }/
  72. & # take the common subsequences
  73. , # apply the filter block from above
  74. {,}% # from each string take length
  75. 0+ # add zero (if empty set was returned)
  76. $-1= # sort and take last element (largest)
  77.  
  78. s=subsequences
  79. f a b c=maximum$map length$filter(isInfixOf c)$intersect(s a)(s b)
  80.  
  81. f[{a_,b_,c_}]:=({z=Subsets[Characters@#]&,s};s=Select[""<>#&/@(z@a⋂z@b),!StringFreeQ[#,c]&];
  82. If[s=={},0,StringLength[s[[-1]]]])
  83.  
  84. f[{"ABCDEF", "CDEFAB", "B"}]
  85.  
  86. f[{"HELLO", "WORLD", "SUCKS"}]
  87.  
  88. f[{"ABCXD", "BCDEF", "CD"}]
  89.  
  90. f[{"X", "Y", "Z"}]
  91.  
  92. chomp(($A,$B,$C)=<>);$A=~s/(.)/($1)?.*?/g;$_=join'',map{$$_}1..$#-and/$C/&&$%<($-=length)and$%=$-while$B=~/.*?$A/g;print$%
  93.  
  94. use feature 'say';
  95. sub L{
  96. ($A,$B,$C,$%)=(@_,0);
  97. $A=~s/(.)/($1)?.*?/g;
  98. $_=join'',map{$$_}1..$#-and/$C/&&$%<($-=length)and$%=$-while$B=~/.*?$A/g;
  99. $%
  100. }
  101. say L(qw/ABCDEF CDEFAB B/);
  102. say L(qw/HELLO WORLD SUCKS/);
  103. say L(qw/ABCXD BCDEF CD/);
  104. say L(qw/X Y Z/);
  105.  
  106. perl LCSoTS.pl
  107. 2
  108. 0
  109. 3
  110. 0
  111.  
  112. ~c~gᵗ⟨⊇ᵛs⟩l|∧
  113.  
  114. ~c A partition of
  115. the input
  116. ᵗ such that its last element
  117. ~g is a list containing one element
  118. ~gᵗ which is replaced with that element
  119. ⟨ ⟩ is a list of two elements
  120. l such that the implicit variable the length of which
  121. | is the output
  122. ⟨⊇ᵛ ⟩ is a subsequence of both elements of the first element of the partition
  123. ⟨ s⟩ and has the last element of the partition as a substring.
  124. | If it's not possible to find such a subsequence,
  125. ∧ leave the output variable unconstrained, so it will default to 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement