@@ -0,0 +1,3311 @@
+import java.io.*;%0Aimport java.util.*;%0A%0Apublic class Solution %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */%0A        Scanner in = new Scanner(System.in);%0A        %0A        int tests = in.nextInt();%0A        for (int t = 0; t %3C tests; t++) %7B%0A            int gridRows = in.nextInt();%0A            in.nextInt(); // ignore grid columns%0A            %0A            String%5B%5D grid = new String%5BgridRows%5D;%0A            for (int i = 0; i %3C gridRows; i++) %7B%0A                grid%5Bi%5D = in.next();%0A            %7D%0A            %0A            int patRows = in.nextInt();%0A            in.nextInt(); // ignore pattern columns%0A            %0A            String%5B%5D pat = new String%5BpatRows%5D;%0A            for (int i = 0; i %3C patRows; i++) %7B%0A                pat%5Bi%5D = in.next();%0A            %7D%0A            %0A            int p = 0, colStart = 0;%0A            int gStart = 0;%0A            boolean found = false;%0A            boolean inWindow = false;%0A            for (int g = 0; g %3C gridRows; g++) %7B%0A                int idx = findSubstring(grid%5Bg%5D, pat%5Bp%5D, colStart);%0A                %0A                if (idx == -1) %7B %0A                    // no pattern string found%0A                    %0A                    if (gridRows - gStart %3C= patRows) %7B %0A                        // it was last possible row to find pattern%0A                        found = false;%0A                        break;%0A                    %7D%0A                    %0A                    inWindow = false;%0A                    p = 0;%0A                    colStart = 0;%0A                    gStart++;%0A                    g = gStart - 1;%0A                    %0A                %7D else if (idx != -1 && inWindow && idx %3E colStart) %7B %0A                    // new pattern position found, but not in expected column, not the first pattern row%0A                    %0A                    inWindow = false;%0A                    p = 0;%0A                    colStart = idx;%0A                    g = gStart - 1;%0A                    %0A                %7D else if (idx != -1) %7B  %0A                    // pattern string found%0A                    %0A                    if (!inWindow) %7B%0A                        colStart = idx;%0A                        inWindow = true;%0A                    %7D%0A                    %0A                    p++;%0A                    %0A                    if (p == patRows) %7B%0A                        found = true;%0A                        break;%0A                    %7D%0A                %7D%0A            %7D%0A            %0A            if (found) %7B%0A                System.out.println(%22YES%22);%0A            %7D else %7B%0A                System.out.println(%22NO%22);%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private static int findSubstring(String str, String pat, int start) %7B%0A        int w = 0;%0A        for (int i = start; i %3C str.length(); i++) %7B%0A            if (str.charAt(i) == pat.charAt(w)) %7B%0A                w++;%0A                %0A                if (w %3E= pat.length()) %7B%0A                    return start;%0A                %7D%0A            %7D else %7B%0A                if (str.length() - start %3C= pat.length()) %7B%0A                    return -1;%0A                %7D%0A                %0A                start++;%0A                i = start - 1;%0A                w = 0;%0A            %7D%0A        %7D%0A        %0A        return -1;%0A    %7D%0A%7D%0A

Create TheGridSearch.java