Problem:
String Search

Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i.

Input

In the first line, a text T is given. In the second line, an integer Q denoting the number of queries is given. In the following Q lines, the patterns P_i are given respectively.

Output

For each question, print 1 if the text includes P_i, or print 0 otherwise.

Constraints

1 ≤ length of T ≤ 1000000

1 ≤ length of P_i ≤ 1000

1 ≤ Q ≤ 10000

The input consists of alphabetical characters and digits

Sample Input 1

aabaaa
4
aa
ba
bb
xyz

Sample Output 1

1
1
0
0

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
	final static int Code = 35;
	
	public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		char[] A, B;
		boolean find = false;
		//??\?????¨??¨??????char???????????£??\
		A = br.readLine().toCharArray();
		int n = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < n; i++){
			B = br.readLine().toCharArray();
			//A??????????????????????????????
			if(A.length < B.length) {
				System.out.println("0");
				continue;
			}
	
			int t = 0;
			int p = 0;
			int BN = search(Code, B.length);
	
			for(int j = 0; j < B.length; j++){
				t = t * Code + A[j];
				p = p * Code + B[j];
			}
			find = Print(t ,p, A, B, BN);
			
			if(find) System.out.println("1");
			else System.out.println("0");
		}
		return;
	}

	public static int search(int x, int n){
		if(n == 0) return 1;
		int ret = search(x * x, n / 2);
		//??????????????????AND?????????
		if((n & 1) == 1) ret *= x;
		return ret;
	}

	public static boolean Print(int t, int p, char[] A, char[] B, int BN){
		//t??¨p?????????????????????????????¨???
		if(t == p) return true;
		for(int i = 1; i < A.length - B.length + 1; i++){
			t = t * Code + A[i + B.length - 1] - A[i - 1] * BN;
			if(t == p) return true;
		}
		return false;
	}
}