Hitung para pendatang yang berdekatan


12

Dimigrasikan dari obrolan

Mengingat bilangan bulat dua non-kosong non-negatif matriks A dan B , menjawab berapa kali A terjadi sebagai berdekatan, mungkin tumpang tindih, submatrix di B .

Contoh / Aturan

0. Mungkin tidak ada orang asing

A :
[[3,1],
[1,4]]

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

Menjawab:
0

1. Submatrices harus bersebelahan

A :
[[1,4],
[3,1]]

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

Jawab:
1(ditandai dengan huruf tebal)

2. Submatri dapat tumpang tindih

A :
[[1,4],
[3,1]]

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

Jawab:
2(masing-masing ditandai tebal dan miring)

3. Matriks (sub) dapat berukuran 1-per-1 dan lebih tinggi

A :
[[3]]

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

Jawab:
3(ditandai dengan huruf tebal)

4. Matriks dapat berupa apa saja

A :
[[3,1,3]]

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

Jawab:
4(dua tebal, dua miring)

Jawaban:


6

Brachylog (v2), 10 byte

{{s\s\}ᵈ}ᶜ

Cobalah online!

Saya suka betapa jelas dan mudahnya program ini ada di Brachylog; Sayangnya, ini bukan byte-wise pendek karena sintaksis metapredicate membutuhkan tiga byte dan harus digunakan dua kali dalam program ini.

Penjelasan

{{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

Jelly , 7 byte

ZẆ$⁺€Ẏċ

Cobalah online!

Bagaimana itu bekerja

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 byte

ZyYC2MX:=XAs

Input A , kemudian B .

Cobalah online! Atau verifikasi semua kasus uji .

Penjelasan

Pertimbangkan input [1,4; 3 1], [3,1,4,5; 6,3,1,4; 5,6,3,1]. Tumpukan ditunjukkan dengan elemen terbaru di bawah ini.

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 byte

øŒεøŒI.¢}O

Cobalah online!

øŒεøŒ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 byte

≢∘⍸⍷

Ini hampir builtin (terima kasih H.PWiz dan ngn ).

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

Alternatif bukan bawaan:

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

Fungsi diad yang mengambil array besar di kanan dan subarray di sebelah kiri.

                  *⍵       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.

Coba di sini .


Anda harus checkout
H.PWiz

Anda dapat menggunakan compose ( ) untuk mempersingkat kereta: +/∘∊⍷atau bahkan≢∘⍸⍷
ngn

1

JavaScript (ES6), 93 byte

Mengambil input sebagai (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

Cobalah online!





1

Arang , 36 27 byte

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

Cobalah online! Jauh lebih pendek sekarang karena Equals berfungsi untuk array lagi. Penjelasan:

   η                        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 byte

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 

Cobalah online!

Cukup mudah. Langkah melalui matriks yang lebih besar, dan periksa apakah matriks yang lebih kecil bisa masuk.

Satu-satunya langkah yang bahkan sedikit rumit adalah pemahaman daftar di baris ke-6, yang bergantung pada konvensi Python untuk mencampur Boolean dan bilangan aritmatika integer.



0

Scala , 151 byte

(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}

Cobalah online!

Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.