Skip to content

Instantly share code, notes, and snippets.

@heatherbooker
Created November 15, 2016 22:45
Show Gist options
  • Save heatherbooker/c8c818bfab53556c932888b4e161c8d2 to your computer and use it in GitHub Desktop.
Save heatherbooker/c8c818bfab53556c932888b4e161c8d2 to your computer and use it in GitHub Desktop.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var R_temp = readLine().split(' ');
var R = R_temp[0];
var C = R_temp[1];
var G = [];
for(var G_i = 0; G_i < R; G_i++){
G[G_i] = readLine();
}
var r_temp = readLine().split(' ');
var r = r_temp[0];
var c = r_temp[1];
var P = [];
for(var P_i = 0; P_i < r; P_i++){
P[P_i] = readLine();
}
search(G, P);
}
}
function search(haystack, needle) {
var firstDigit = needle[0][0];
for (var i = 0; i < haystack.length - needle.length; i++) {
for (var j = 0; j < haystack[i].length - needle[0].length; j++) {
var digit = haystack[i][j];
if (firstDigit === digit) {
if (checkIfMatch(haystack, needle, i, j)) {
console.log('YES');
return;
}
}
}
}
console.log('NO');
}
function checkIfMatch(haystack, needle, hayRow, hayCol) {
var needleRow = 0;
var needleCol = 0;
var numOfColsInNeedle = needle[0].length;
for (var i = hayRow; i < haystack.length; i++) {
for (var j = hayCol; j < haystack[0].length; j++) {
if (haystack[i][j] === needle[needleRow][needleCol]) {
if (needleCol < numOfColsInNeedle - 1) {
needleCol++;
} else {
if(needleRow === needle.length -1 && needleCol === numOfColsInNeedle -1){
return true;
}
needleCol = 0;
needleRow++;
break;
}
} else {
return false;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment