唯一可移动的子序列


25

介绍

考虑一个整数序列及其一个子序列,例如A = [4 2 2 4 4 6 5]B = [2 4 5]。我们要按顺序从A中删除B的元素,并且有几种方法可以这样做:

A = 4 2 2 4 4 6 5
B =   2   4     5
 -> 4   2   4 6

A = 4 2 2 4 4 6 5
B =     2 4     5
 -> 4 2     4 6

A = 4 2 2 4 4 6 5
B =   2     4   5
 -> 4   2 4   6

A = 4 2 2 4 4 6 5
B =     2   4   5
 -> 4 2   4   6

在所有情况下,其余序列都是相同的[4 2 4 6]。如果发生这种情况,我们说B可以 A 唯一移除

任务

您的输入是两个非负整数序列AB,其中B保证是A的子序列。输入可能相等,也可能为空。您可以按照任何想要的顺序以任何合理的格式使用它们。

如果B可从A唯一移除,则您的输出应为真实值,否则为假值。

规则和计分

您可以编写完整的程序或函数。最低字节数获胜。

测试用例

[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True

Answers:


6

Haskell,90 84字节

a%[]=[a]
(t:z)%x@(s:y)=[a|a<-z%y,t==s]++[t:s|s<-z%x]
_%_=[]
((all=<<(==).head).).(%)

不确定地构造所有可能的减去序列的列表,并检查它们是否全部相等。

用法:

*Main> mapM_(\(a,b)->let r=(((all=<<(==).head).).(%)) a b in putStrLn$concat[show a," ",show b," -> ",show r]) [([],[]), ([0,3],[]), ([1,0,1],[1]), ([0,2],[0,2]), ([2,2,1,1,2,2,2],[2,1]), ([4,2,2,4,4,6,5],[4,5]), ([10,5,10,10,5,10],[10,5,10]), ([4,2,2,4,4,6,5],[2,4,5]), ([1,1,1,0,0,0,1,1,1,0],[1,0,1,1]), ([0,1,0,0,0,0,1,1,0,1],[1,0,1,1]), ([0,4,0,0,4,1,4,2,2],[0,0,0,1,4]), ([0,2,2,25,0,2,2,26,0,0,2],[2,0,0,0,2]), ([1,1,1,3,2,1,3,2,2,3,3,2],[1,1,2,3,2]), ([0,3,2,0,1,3,2,0,0,0,3,2],[0,1,2,0,3]), ([5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5],[2,7,5,7,7,2]), ([5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1],[4,0,1,1,2,1]), ([0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4],[0,1,0,0,2,0,1,4])]
[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True

感谢@Zgarb节省了6个字节!


您可以重新整理内容,并x%_=x针对的第二种情况%。另外,我认为主要功能将以有针对性的形式缩短。
Zgarb '16

@Zgarb x%_=x无法使用,因为类型不匹配,但是会_%_=[]保存一个字节。
2016年

4

的JavaScript(ES6),141 152 156 159

递归函数-相当长

f=(a,b,i=0,j=0,r=a,S=new Set)=>(1/b[j]?1/a[i]&&f(a,b,i+1,j,r,S,a[i]-b[j]||f(a,b,i+1,j+1,r=[...r],r[i]=S)):S.add(0+r.filter(x=>1/x)),S.size<2)

少打高尔夫球

f=(a, b, 
   i = 0, // current position to match in a
   j = 0, // current position to match in b
   r = a, // current result so far, A with elements of B removed - start == A
   S = new Set // set of possible "A removed B"
) => (
    1 / b[j] // check if j is still inside B
    ? 1 / a[i] // if i is inside A
      && (
        // in any case try to find current element of B in the remaining part of A
        f(a, b, i+1, j, r, S),
        a[i] == b[j] // if match was found between A and B
        && 
          // mark current element in a copy of r and 
          // look for the next element of B in the remaining part of A
          f(a, b, i+1, j+1, r=[...r], r[i]=S),
      )
    // else - j is not inside B, we have a solution in r
    // use filter to get a copy without the marked elements
    //  (note: 1/any_number == number_not_0, 1/Object == NaN)
    // then convert to string, to use as a key in S
    : S.add(0+a.filter(x=>1/x)),
    S.size<2 // return true if S has only 1 element
)  

测试

f=(a,b,i=0,j=0,r=a,S=new Set)=>(1/b[j]?1/a[i]&&f(a,b,i+1,j,r,S,a[i]-b[j]||f(a,b,i+1,j+1,r=[...r],r[i]=S)):S.add(0+r.filter(x=>1/x)),S.size<2)


out=(...x)=>O.textContent+=x.join` `+'\n'
;`[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True`
.split('\n').forEach(t=>{
  var [a,b,_,k]=t.match(/\S+/g)
  var r=f(eval(a),eval(b))
  out(r==(k[0]=='T')?'OK':'KO',a,b,r,k)
})
<pre id=O></pre>



3

的JavaScript(ES6),116个 114 113字节

返回falsetrue

(a,b,p)=>((F=(a,i,m)=>1/b[i]?a.map((n,j)=>m>j|n-b[i]||F(a.filter((_,k)=>j-k),i+1,j)):p?r|=p!=a:p=a+'')(a,r=0),!r)

格式化和评论

(                                     // given:
  a, b,                               // - a, b = input arrays
  p                                   // - p = reference pattern, initially undefined
) => (                                //
  (F = (                              // F is our recursive search function, with:
    a,                                // - a = current working copy of the main array
    i,                                // - i = index in 'b'
    m                                 // - m = minimum index of matching values in 'a'
  ) =>                                //
    1 / b[i] ?                        // if we haven't reached the end of 'b':
      a.map((n, j) =>                 //   for each element 'n' at index 'j' in 'a':
        m > j | n - b[i] ||           //     if 'n' is a valid matching value,
        F(                            //     do a recursive call to F(), using:
          a.filter((_, k) => j - k),  //     - a copy of 'a' without the current element
          i + 1,                      //     - the next index in 'b'
          j                           //     - 'j' as the new minimum index in 'a'
        )                             //
      )                               //
    :                                 // else:
      p ?                             //   if the reference pattern is already set:
        r |= p != a                   //     check if it's matching the current 'a'
      :                               //   else:
        p = a + ''                    //     set the current 'a' as the reference pattern
  )(a, r = 0),                        //  initial call to F() + initialization of 'r'
  !r                                  //  yields the final result
)                                     //

测试用例


哇!我试图找到一种方法来递减A的副本,但没有成功
edc65


1

JavaScript(Firefox 30 +),159147字节

f=(a,b,i=f(a,b,0))=>i?i.every(x=>x+""==i[0]):b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1),0))[...a.slice(0,i-1),...x]]:[a]

这是两种替代方法,都是匿名函数:

(a,b,f=(a,b,i=0)=>b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1)))[...a.slice(0,i-1),...x]]:[a],c=f(a,b))=>c.every(x=>x+""==c[0])
(a,b,f=(a,b,i=0)=>b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1)))[...a.slice(0,i-1),...x]]:[a])=>new Set(f(a,b).map(btoa)).size<2

测试片段


我也喜欢
摘要

1

Mathematica,128个字节

h=Length;n=ToExpression;g=ToString;y=Array;h@Union@ReplaceList[#2,n@Riffle[y["a"<>g@#<>"___"&,t=h@#+1],#]->n@y["a"<>g@#&,t]]==1&

具有两个列表参数的未命名函数,其中第一个是子序列,第二个是完整序列;输出TrueFalse

核心部分是以下顺序,出于可读性考虑而不再赘述:

ReplaceList[#2, ToExpression @
  Riffle[
    Array["a" <> ToString@# <> "___" &, Length@# + 1]
    , #
  ] -> ToExpression @ 
    Array["a" <> ToString@#& , Length@# + 1 ]
]

这里#代表子序列,例如{2,4,5}。第一条Array命令创建一个字符串列表,如{"a1___","a2___","a3___","a4___"},然后Riffle将其与d一起#生成一个奇怪的列表,如{"a1___",2,"a2___",4,"a3___",5,"a4___"};然后将此列表转换为实际的Mathematica表达式。例如{2,4,5},此核心代码的部分评估为

ReplaceList[#2, {a1___,2,a2___,4,a3___,5,a4___} -> {a1,a2,a3,a4}]

确切地列出了删除子序列的所有可能方法 {2,4,5}#2独自离开该列表的其余部分。

生成此列表后,我们只需使用删除重复项,Union然后测试结果输出的长度是否为1。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.