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 C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOTFOUND 256

void Search(char *P,int Plen,char *T,int Tlen,int *MoveP)
{
    int i,initMP[NOTFOUND],cnt = 0,j,flg,nflg=0;
    
    /* 配列の初期化 */
    for( i=1 ; i<=Plen ; i++)
    {
        if(i-1 < MoveP[(unsigned int)P[Plen-i]])
        {
            MoveP[(unsigned int)P[Plen-i]] = i-1;
            //printf("MoveP = %d,",MoveP[(unsigned int)P[Plen-i]]);
            initMP[cnt] = (unsigned int)P[Plen-i];
            cnt++;
        }
    }
    //printf("\n");
    
    /* 変更のよちあり・？？ */
    for( i=0 ; i<=Tlen-Plen ; i++ )
    {
        flg = 0;
        if(T[Plen+i-1] == P[Plen-1])
        {
            for( j=1 ; j<=Plen ; j++)
            {
                //printf("%c,%c\n",T[Plen+i-j],P[Plen-j]);
                if(T[Plen+i-j] != P[Plen-j])
                {
                    flg = 1;
                    //printf("P = %c,i = %d =>",P[Plen-j],i);
                    i += MoveP[(unsigned int)P[Plen-j]];
                    //printf("%d,i++%d\n",i,MoveP[(unsigned int)P[Plen-j]]);
                    break;
                }
            }
            if(flg == 0)
            {
                printf("1\n");
                nflg = 1;
                break;
            }
        }
    }
    /* ここまで、確認すべし */
    
    if(nflg == 0)
    {
        printf("0\n");
    }
    /* MovePを初期化 */
    for( i=0 ; i<cnt ; i++)
    {
        MoveP[initMP[i]] = NOTFOUND;
    }
    
}


int main(){
    int n,i,j,Tlen,Plen,MoveP[NOTFOUND];
    char T[1000000],P[1000];
    scanf("%s",T);
    scanf("%d",&n);
    Tlen = strlen(T);
    
    /* ずらし表の初期化(NOTFOUNDであればPlen分進ませる) */
    for( i=0 ; i<NOTFOUND ; i++)
    {
        MoveP[i] = NOTFOUND;
    }
    
    for( i=0 ; i<n ; i++ )
    {
        scanf("%s",P);
        Plen = strlen(P);
        Search(P,Plen,T,Tlen,MoveP);
    }
}