Under construction

Still being built. Things may change

Hamming distance queries

Interview

Time limit6sMemory limit512 MB

Summary
Given binary strings a and b, answer queries asking for the Hamming distance between a substring of a and a substring of b.
Level

Medium5 of 10

Topics
Prefix sum, String, Array, Bit manipulation
Solved
No attempts yet

Problem

The Hamming distance between two binary strings ss and tt of equal length is the number of positions where the two values differ. For example, the Hamming distance between "00111" and "10101" is 2.

You are given binary strings aa and bb. Write a program that answers the following query.

  • p1 p2 len: print the Hamming distance between the substrings ap1ap1+1…ap1+len−1a_{p_1} a_{p_1+1} \ldots a_{p_1+len-1} and bp2bp2+1…bp2+len−1b_{p_2} b_{p_2+1} \ldots b_{p_2+len-1}.

String indices start at 0.

Input

The first line has the binary string aa and the second line has the binary string bb. The length of each string is a positive integer at most 200,000.

The third line has the number of queries qq (1≤q≤400,0001 \le q \le 400{,}000). Each of the next qq lines has one query as p1p_1, p2p_2, and lenlen separated by spaces (1≤len1 \le len, 0≤p1≤∣a∣−len0 \le p_1 \le |a| - len, 0≤p2≤∣b∣−len0 \le p_2 \le |b| - len).

Output

Print the answer to each query on its own line, in the order the queries are given.

Examples6

  1. Example 1

    Input
    101010
    11110000
    3
    0 0 3
    2 3 4
    5 7 1
    
    Expected output
    1
    1
    0
    
  2. Example 2

    Input
    10001010101011001010100101010011010
    101010100101001010100100101010
    5
    0 0 12
    3 9 7
    6 4 15
    12 15 10
    13 3 20
    
    Expected output
    5
    4
    3
    5
    13
    
  3. Example 3

    Input
    0
    1
    1
    0 0 1
    
    Expected output
    1
    
  4. Example 4

    Input
    1111100000
    1111100000
    4
    0 0 10
    0 5 5
    5 0 5
    3 3 4
    
    Expected output
    0
    5
    5
    0
    
  5. Example 5

    Input
    1111111111111111111111111111111111111111111111111111111111111111111111
    0000000000000000000000000000000000000000000000000000000000000000000000
    5
    0 0 70
    0 0 64
    6 6 64
    0 6 63
    69 69 1
    
    Expected output
    70
    64
    64
    63
    1
    
  6. Example 6

    Input
    1110010011000110000000011100110011101101000101100000011110011011010000001100100110110011000100001101110000001110101001000001101000
    0101111100001010000111000011001011000111000101100011001101010101100000111111100101000111011000111011101001010001100100111011111100
    8
    0 0 130
    1 0 129
    0 1 129
    63 1 65
    64 64 66
    65 3 64
    2 66 64
    129 129 1
    
    Expected output
    67
    56
    63
    31
    35
    31
    34
    0