Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da99bb8

Browse files
the-grid-search
1 parent 1dc9677 commit da99bb8

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎the-grid-search.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', _ => {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
const getIndexes = (pattern, match) =>{
28+
let indexes = [];
29+
let i=0;
30+
let index = pattern.indexOf(match, i);
31+
while(index >= 0){
32+
indexes.push(index);
33+
i++;
34+
index = pattern.indexOf(match, i);
35+
}
36+
return Array.from(new Set(indexes));
37+
}
38+
function gridSearch(G, P) {
39+
let G_length = G.length;
40+
let P_length = P.length;
41+
// console.log('G_length :', G_length, ' P_length:', P_length);
42+
for (let i = 0; i < G.length; i++) {
43+
let j = i, p = 0;;
44+
let currentGrid = G[i + p];
45+
let currentPattern = P[p];
46+
let indexes = getIndexes(currentGrid, currentPattern);
47+
// console.log(indexes);
48+
for (let k = 0; k < indexes.length; k++) {
49+
let pos = indexes[k];
50+
if (pos !== -1) {
51+
for (p = 1; p < P.length; p++) {
52+
currentGrid = G[i + p];
53+
currentPattern = P[p];
54+
if (getIndexes(currentGrid, currentPattern).indexOf(pos) === -1) {
55+
break;
56+
}
57+
}
58+
}
59+
if (p === P_length)
60+
return "YES";
61+
}
62+
}
63+
return "NO";
64+
}
65+
66+
function main() {
67+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
68+
69+
const t = parseInt(readLine(), 10);
70+
71+
for (let tItr = 0; tItr < t; tItr++) {
72+
const RC = readLine().split(' ');
73+
74+
const R = parseInt(RC[0], 10);
75+
76+
const C = parseInt(RC[1], 10);
77+
78+
let G = [];
79+
80+
for (let i = 0; i < R; i++) {
81+
const GItem = readLine();
82+
G.push(GItem);
83+
}
84+
85+
const rc = readLine().split(' ');
86+
87+
const r = parseInt(rc[0], 10);
88+
89+
const c = parseInt(rc[1], 10);
90+
91+
let P = [];
92+
93+
for (let i = 0; i < r; i++) {
94+
const PItem = readLine();
95+
P.push(PItem);
96+
}
97+
98+
let result = gridSearch(G, P);
99+
100+
ws.write(result + "\n");
101+
}
102+
103+
ws.end();
104+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /