计算连续的子矩阵


12

从聊天迁移

给定两个非空非负整数矩阵,回答的次数出现作为连续的,可能重叠,子矩阵

范例/规则

0.可能没有任何子矩阵


[[3,1],
[1,4]]

B
[[1,4],
[3,1]]

回答:
0

1.子矩阵必须是连续的


[[1,4],
[3,1]]

B
[[3,1,4,0,5],
[6,3,1,0,4],
[5,6,3,0,1]]

答:(
1以粗体显示)

2.子矩阵可能重叠


[[1,4],
[3,1]]

B
[[3,1,4,5],
[6,3,1,4],
[5,6,3,1]]

答:(
2分别以粗体和斜体标出)

3.(子)矩阵的大小可能为1-by-1或更大


[[3]]

B
[[3,1,4,5],
[6,3,1,4],
[5,6,3,1]]

答:(
3以粗体显示)

4.矩阵可以是任何形状


[[3,1,3]]

[[3,1,3,1,3,1,3,1,3]]

答:(
4两个粗体,两个斜体)

Answers:


6

Brachylog(v2),10个字节

{{s\s\}ᵈ}ᶜ

在线尝试!

我喜欢Brachylog中该程序的清晰明了;不幸的是,它不是那么短的字节长度,因为元谓词语法占用三个字节,并且在该程序中必须使用两次。

说明

{{s\s\}ᵈ}ᶜ
  s         Contiguous subset of rows
   \s\      Contiguous subset of columns (i.e. transpose, subset rows, transpose)
 {    }ᵈ    The operation above transforms the first input to the second input
{       }ᶜ  Count the number of ways in which this is possible

5

果冻,7个字节

ZẆ$⁺€Ẏċ

在线尝试!

怎么运行的

ZẆ$⁺€Ẏċ  Main link. Arguments: B, A

  $      Combine the two links to the left into a monadic chain.
Z          Zip; transpose the matrix.
 Ẇ         Window; yield all contiguous subarrays of rows.
   ⁺     Duplicate the previous link chain.
    €    Map it over the result of applying it to B.
         This generates all contiguous submatrices of B, grouped by the selected
         columns of B.
     Ẏ   Tighten; dump all generated submatrices in a single array.
      ċ  Count the occurrences of A.

4

MATL,12字节

ZyYC2MX:=XAs

输入是,然后

在线尝试!验证所有测试用例

说明

考虑输入[1,4; 3 1][3,1,4,5; 6,3,1,4; 5,6,3,1]。堆栈显示在下面,带有最新元素。

Zy    % Implicit input: A. Push size as a vector of two numbers
      % STACK: [2 2]
YC    % Implicit input: B. Arrange sliding blocks of specified size as columns,
      % in column-major order
      % STACK: [3 6 1 3 4 1;
                6 5 3 6 1 3;
                1 3 4 1 5 4;
                3 6 1 3 4 1]
2M    % Push input to second to last function again; that is, A
      % STACK: [3 6 1 3 4 1;
                6 5 3 6 1 3;
                1 3 4 1 5 4;
                3 6 1 3 4 1],
               [1 4;
                3 1]                    
X:    % Linearize to a column vector, in column-major order
      % STACK: [3 6 1 3 4 1;
                6 5 3 6 1 3;
                1 3 4 1 5 4;
                3 6 1 3 4 1],
               [1;
                3;
                4;
                1]  
=     % Test for equality, element-wise with broadcast
      % STACK: [0 0 1 0 0 1
                0 0 1 0 0 1;
                0 0 1 0 0 1;
                0 0 1 0 0 1]
XA    % True for columns containing all true values
      % STACK: [0 0 1 0 0 1]
s     % Sum. Implicit display
      % STACK: 2

2

05AB1E,10个字节

øŒεøŒI.¢}O

在线尝试!

øŒεøŒI.¢}O     Full program. Takes 2 matrices as input. First B, then A.
øŒ             For each column of B, take all its sublists.
  ε     }      And map a function through all those lists of sublists.
   øŒ          Transpose the list and again generate all its sublists.
               This essentially computes all sub-matrices of B.
     I.¢       In the current collection of sub-matrices, count the occurrences of A.
         O     At the end of the loop sum the results.

2

Dyalog APL,6个 4字节

≢∘⍸⍷

这是近一个内置(感谢H.PWizNGN)。

  ⍷       Binary matrix containing locations of left argument in right argument
≢∘⍸       Size of the array of indices of 1s

可选的非内置的:

{+/,((*⍺)≡⊢)⌺(⍴⍺)*⍵}

双向函数,将大数组放在右侧,子数组放在左侧。

                  *⍵       exp(⍵), to make ⍵ positive.
    ((*⍺)≡⊢)⌺(⍴⍺)        Stencil;
                            all subarrays of ⍵ (plus some partial subarrays
                            containing 0, which we can ignore)
               ⍴⍺             of same shape as ⍺
     (*⍺)≡⊢                   processed by checking whether they're equal to exp(⍺).
                           Result is a matrix of 0/1.
   ,                     Flatten
 +/                      Sum.

在这里尝试。


您应该结帐
H.PWiz,

你可以使用撰写(),以缩短列车:+/∘∊⍷甚至≢∘⍸⍷
NGN

1

JavaScript(ES6),93个字节

将输入作为(A)(B)

a=>b=>b.map((r,y)=>r.map((_,x)=>s+=!a.some((R,Y)=>R.some((v,X)=>v!=(b[y+Y]||0)[x+X]))),s=0)|s

在线尝试!





1

木炭36 27字节

IΣ⭆η⭆ι⁼θE✂ηκ⁺Lθκ¹✂νμ⁺L§θ⁰μ¹

在线尝试!现在,Equals再次可用于数组,但要短得多。说明:

   η                        Input array B
  ⭆                         Mapped over rows and joined
     ι                      Current row
    ⭆                       Mapped over columns and joined
       θ                    Input array A
      ⁼                     Is equal to
          η                 Input array B
         ✂                  Sliced
                ¹           All elements from
           κ                Current row index to
             L              Length of
              θ             Input array A
            ⁺               Plus
               κ            Current row index
        E                   Mapped over rows
                  ν         Current inner row
                 ✂          Sliced
                          ¹ All elements from
                   μ        Current column index to
                     L      Length of
                       θ    Input array A
                      §     Indexed by
                        ⁰   Literal 0
                    ⁺       Plus
                         μ  Current column index
 Σ                          Digital sum
I                           Cast to string
                            Implicitly printed

0

Python 2,211字节

a,b=input()
l,w,L,W,c=len(a),len(a[0]),len(b),len(b[0]),0
for i in range(L):
 for j in range(W):
  if j<=W-w and i<=L-l:
   if not sum([a[x][y]!=b[i+x][j+y]for x in range(l)for y in range(w)]):
    c+=1
print c 

在线尝试!

非常坦率的。逐步浏览较大的矩阵,然后检查较小的矩阵是否适合。

唯一甚至有些棘手的步骤是第6行中的列表理解,它依赖于Python混合布尔和整数算术的约定。



0

Scala,151字节

(a,b)=>{(0 to b.size-a.size).map(i=>(0 to b(0).size-a(0).size).count(j=>{var k=i-1
a.forall(c=>{var l=j-1;k+=1
c.forall(d=>{l+=1
b(k)(l)==d})})})).sum}

在线尝试!

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.