Problem:
Pattern Search

Find places where a R × C pattern is found within a H × W region. Print top-left coordinates (i, j) of sub-regions where the pattern found. The top-left and bottom-right coordinates of the region is (0, 0) and (H-1, W-1) respectively.

Input

In the first line, two integers H and W are given. In the following H lines, i-th lines of the region are given.

In the next line, two integers R and C are given. In the following R lines, i-th lines of the pattern are given.

output

For each sub-region found, print a coordinate i and j separated by a space character in a line. Print the coordinates in ascending order of the row numbers (i), or the column numbers (j) in case of a tie.

Constraints

1 ≤ H, W ≤ 1000

1 ≤ R, C ≤ 1000

The input consists of alphabetical characters and digits

Sample Input 1

4 5
00010
00101
00010
00100
3 2
10
01
10

Sample Output 1

0 3
1 2

Solution in C# (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class SearchString
    {
        readonly int MOD = 1000000007;
        readonly int C1 = 401;
        readonly int C2 = 397;

        readonly string[] txt, pat;
        readonly int H, W, R, C;

        public SearchString(string[] txt, int h, int w, string[] pat, int r, int c)
        {
            this.txt = txt; H = h; W = w;
            this.pat = pat; R = r; C = c;
        }

        public void PrintIndex()
        {
            long[,] txtTable = GenHash(txt, H, W);
            long[,] patTable = GenHash(pat, R, C);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i <= H - R; i++)
            {
                for (int j = 0; j <= W - C; j++)
                {
                    if (txtTable[i, j] == patTable[0, 0]) sb.AppendLine(i + " " + j);
                }
            }
            Console.Write(sb);
        }

        private long[,] GenHash(string[] v, int h, int w)
        {
            long[,] h1 = new long[h, w];
            long[,] h2 = new long[h, w];

            long cn1 = 1, cn2 = 1;

            for (int i = 0; i < C; i++) cn1 = (cn1 * C1) % MOD;
            for (int i = 0; i < R; i++) cn2 = (cn2 * C2) % MOD;

            for (int i = 0; i < h; i++)
            {
                long hs = 0;

                for (int j = 0; j < C; j++) hs = (hs * C1 + v[i][j]) % MOD;

                for (int j = 0; j <= w - C; j++)
                {
                    h1[i, j] = hs;

                    if (j + C < w) hs = (hs * C1 - v[i][j] * cn1 + v[i][j + C]) % MOD;

                    hs = (hs < 0) ? hs + MOD : hs;
                }
            }

            for (int i = 0; i < w; i++)
            {
                long hs = 0;

                for (int j = 0; j < R; j++) hs = (hs * C2 + h1[j, i]) % MOD;

                for (int j = 0; j <= h - R; j++)
                {
                    h2[j, i] = hs;

                    if (j + R < h) hs = (hs * C2 - h1[j, i] * cn2 + h1[j + R, i]) % MOD;

                    hs = (hs < 0) ? hs + MOD : hs;
                }
            }
            return h2;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            int H = input[0];
            int W = input[1];

            string[] text = new string[H];
            for (int i = 0; i < H; i++) text[i] = Console.ReadLine();

            input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            int R = input[0];
            int C = input[1];

            string[] pattarn = new string[R];
            for (int i = 0; i < R; i++) pattarn[i] = Console.ReadLine();

            SearchString SS = new SearchString(text, H, W, pattarn, R, C);

            SS.PrintIndex();
        }
    }
}