-
Notifications
You must be signed in to change notification settings - Fork 35
/
TheGridSearch.cs
97 lines (73 loc) · 2.52 KB
/
TheGridSearch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// https://www.hackerrank.com/challenges/the-grid-search/problem
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
// Complete the gridSearch function below.
static string gridSearch(string[] G, string[] P)
{
var firsLine = P[0];
for (var row = 0; row < G.Length; row++)
{
var indexOf = G[row].IndexOf(firsLine);
while (indexOf >= 0)
{
if (IsMatch(G, P, row, indexOf))
return "YES";
indexOf = G[row].IndexOf(firsLine, indexOf + 1);
}
}
return "NO";
}
private static bool IsMatch(string[] g, string[] p, int firstRow, int firstCol)
{
if (firstCol + p[0].Length > g[0].Length ||
firstRow + p.Length > g.Length) return false;
for (var row = 0; row < p.Length; row++)
for (var col = 0; col < p[row].Length; col++)
if (g[row + firstRow][col + firstCol] != p[row][col])
return false;
return true;
}
static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int t = Convert.ToInt32(Console.ReadLine());
for (int tItr = 0; tItr < t; tItr++)
{
string[] RC = Console.ReadLine().Split(' ');
int R = Convert.ToInt32(RC[0]);
int C = Convert.ToInt32(RC[1]);
string[] G = new string[R];
for (int i = 0; i < R; i++)
{
string GItem = Console.ReadLine();
G[i] = GItem;
}
string[] rc = Console.ReadLine().Split(' ');
int r = Convert.ToInt32(rc[0]);
int c = Convert.ToInt32(rc[1]);
string[] P = new string[r];
for (int i = 0; i < r; i++)
{
string PItem = Console.ReadLine();
P[i] = PItem;
}
string result = gridSearch(G, P);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}