Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Testcase #1
- Testcase #2
- Testcase #3
- Testcase #4
- ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
- ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
- ABCDEF
- CDEFAB
- B
- 2
- ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
- HELLO
- WORLD
- SUCKS
- 0
- ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
- ABCXD
- BCDEF
- CD
- 3
- ⌈/0,⍴¨m/⍨∨/¨⍞∘⍷¨m←⊃∩/{/∘⍵¨↓⍉(z/2)⊤⍳2*z←⍴⍵}¨⍞⍞
- X
- Y
- Z
- 0
- s(A,[H|R]):-(A=[H|T],s(T,R);s(A,R)).
- s([],[]).
- r(A,B,C,O):-(r(A,B,C,0,O),!;O=0).
- 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).
- ?- r("abcxd","bcdef","cd",O).
- O = 3.
- from itertools import*
- def f(a,b,c):
- f=lambda x:set(''.join(z)for y in range(len(x))for z in combinations(x,y))
- return max(len(x)for x in f(a).intersection(f(b))if c in x or not x)
- k⊇ᵐ=h.s~t?∨0
- k⊇ᵐ=h.s~t?∨0
- k Take the input, apart from the last element.
- ⊇ᵐ Find the longest subsequence of each element
- = such that those subsequences are equal
- h and that subsequence
- s has a substring
- ~t? that's the last element of the input.
- . If you can, return the subsequence.
- ∨0 If all else fails, return 0.
- n/)`{?)}+{['']1/{`{1$+}+%}/}/&,{,}%0+$-1=
- n/ # split the input at newline into three strings
- )` # take the last one and stringify it
- {?)}+ # and build a code block to filter for any string
- # which has that one as a substring (for later use)
- { # now loop over the remaining two strings and
- # build their subsequences
- [''] # start with the set of the empty string
- 1/{ # loop over each character of the string
- `{ # apply code block to each member of the set
- 1$+ # copy member and add the current character
- }+% # -> set is updated with latest character
- }/
- }/
- & # take the common subsequences
- , # apply the filter block from above
- {,}% # from each string take length
- 0+ # add zero (if empty set was returned)
- $-1= # sort and take last element (largest)
- s=subsequences
- f a b c=maximum$map length$filter(isInfixOf c)$intersect(s a)(s b)
- f[{a_,b_,c_}]:=({z=Subsets[Characters@#]&,s};s=Select[""<>#&/@(z@a⋂z@b),!StringFreeQ[#,c]&];
- If[s=={},0,StringLength[s[[-1]]]])
- f[{"ABCDEF", "CDEFAB", "B"}]
- f[{"HELLO", "WORLD", "SUCKS"}]
- f[{"ABCXD", "BCDEF", "CD"}]
- f[{"X", "Y", "Z"}]
- chomp(($A,$B,$C)=<>);$A=~s/(.)/($1)?.*?/g;$_=join'',map{$$_}1..$#-and/$C/&&$%<($-=length)and$%=$-while$B=~/.*?$A/g;print$%
- use feature 'say';
- sub L{
- ($A,$B,$C,$%)=(@_,0);
- $A=~s/(.)/($1)?.*?/g;
- $_=join'',map{$$_}1..$#-and/$C/&&$%<($-=length)and$%=$-while$B=~/.*?$A/g;
- $%
- }
- say L(qw/ABCDEF CDEFAB B/);
- say L(qw/HELLO WORLD SUCKS/);
- say L(qw/ABCXD BCDEF CD/);
- say L(qw/X Y Z/);
- perl LCSoTS.pl
- 2
- 0
- 3
- 0
- ~c~gᵗ⟨⊇ᵛs⟩l|∧
- ~c A partition of
- the input
- ᵗ such that its last element
- ~g is a list containing one element
- ~gᵗ which is replaced with that element
- ⟨ ⟩ is a list of two elements
- l such that the implicit variable the length of which
- | is the output
- ⟨⊇ᵛ ⟩ is a subsequence of both elements of the first element of the partition
- ⟨ s⟩ and has the last element of the partition as a substring.
- | If it's not possible to find such a subsequence,
- ∧ leave the output variable unconstrained, so it will default to 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement