Language: C++

#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstring> //memcpy
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <map>
//#include <unordered_map> //hash func.
#include <fstream> //ifstream, ofstream
#include <iterator> //insert_iterator::inserter


//#define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert

using namespace std;


#define dout cout
//If u wanna output to a text file instead of standard output, plz define OUTPUTFILE.
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif


#define din cin
//If u wanna input from a text file instead of standard input, plz define INPUTFROMTEXTFILE???.
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define scan(A) scanf("%d", &(A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl
//#define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) << A[j] << " ";} cout << endl

#define sign(x) ((x)>0)-((x)<0) //x<0: -1, x=0: 0, x>0: +1
#define p(i) (i)/2
#define l(i) (i)*2
#define r(i) (i)*2+1

int dx[] = {1,-1, 0, 0, 1, 1,-1,-1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0,-1, 1,-1, 1, 1,-1};

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;

const int INF = (int)2e9+10;
const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow.
const ull INF_ULL = (ull)1e19-1ULL;
const int NONE = -1;
const ll MOD = (ll)1e9+7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????

const int N_MAX = 1000010; //num of vertex or element
const int M_MAX = 10010; //num of edge
const int DATA_MAX = 1010;

int N;

char T[N_MAX];
char* P[M_MAX];
vi placeOfP[M_MAX];


struct PMA { //Pattern Matching Automaton
#ifdef debug
    string str = "";
#endif
    PMA* next[128]; //next[0] = failure link
    vi accept;
    
    PMA() { fill(next, next+128, (PMA*)0); }
};


vi set_union(const vi &a, const vi &b) {
    vi res;
    set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res) );
    return res;
}



PMA* buildPMA(char* P[], int numOfP) {
    PMA* root = new PMA;
#ifdef debug
    root->str = "root ";
#endif
    
    //construct PMA according to P[i]
    rep(i,0,numOfP) {
        PMA* t = root;
        
        for (int j=0; P[i][j]; j++) {
            char c = P[i][j];
            if(t->next[c]==NULL) {
                t->next[c] = new PMA;
#ifdef debug
                t->next[c]->str = t->str + c;
#endif
            }
            t = t->next[c];
        }
        
        t->accept.push_back(i);
    }
    
    
    //make failure link for all node using BFS
    queue<PMA*> Q;
    
    //failure link of root's child ---> root
    rep(c,'0','z'+1) {
        if(root->next[c]!=NULL) {
            root->next[c]->next[0] = root;
            Q.push(root->next[c]);
        }
        else root->next[c] = root; //all other character except for P[] ---> root
    }
    
    //BFS
    while(!Q.empty()) {
        
        PMA* now = Q.front(); Q.pop();
        
        rep(c,'0','z'+1) {
            if(now->next[c]!=NULL) {
                Q.push(now->next[c]);
                
                PMA* failure = now->next[0];
                while(failure->next[c]==NULL) failure = failure->next[0]; //c??????????????????????????§failure link?????????
                
                now->next[c]->next[0] = failure->next[c]; //now->next[c]???failure link?????????
                now->next[c]->accept = set_union(now->next[c]->accept, failure->next[c]->accept); //now->next[c]????????????????????????failure link?????????????????????????¶????
            }
        }
    }
    
    return root;
}



void match(PMA* v, char *text, vi result[]) {
    
    int lenT = strlen(text);
    
    rep(i,0,lenT) {
        char c = text[i];
        
        while(v->next[c]==NULL) {
            v = v->next[0];
        }
        
        v = v->next[c];
        
        rep(j,0,v->accept.size()) {
            result[v->accept[j]].push_back(i+1-strlen(P[v->accept[j]]));
        }
    }
    
    return;
}



void display_DFS(PMA* r) {
    
    dout << "-----\n";
    disp(r->str);
    
    dout << "accept = ";
    rep(i,0,r->accept.size()) {
        dout << r->accept[i] << " ";
    }
    dout << endl;
    
    rep(c,'0','z'+1) {
        if(r->next[c]!=NULL && r->next[c]!=r) {
            display_DFS(r->next[c]);
        }
    }
}


int main() {
    
    //cin, cout????????????  ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
    cin.tie(0); //cin??¨cout??????????????????
    ios::sync_with_stdio(false); //iostream??¨stdio??????????????????
    
    //read input data
    scanf("%s", T);
    
    int Q;
    scanf("%d ", &Q);
    rep(i,0,Q) {
        P[i] = (char *)malloc(sizeof(char)*DATA_MAX);
        scanf("%s", P[i]);
    }
    
#ifdef debug
    disp(T);
    disp(strlen(T));
    rep(i,0,Q) { disP(P[i]); disp(strlen(P[i])); }
#endif
    
    
    //------------------------------------------------------------------------------------------
#ifdef debug
    //start timer
    auto startTime = chrono::system_clock::now();
#endif
    //------------------------------------------------------------------------------------------
    
    
    //Aho-Corasick Algorithm
    PMA* root = buildPMA(P, Q);
    
#ifdef debug
    display_DFS(root);
#endif
    
    
    match(root, T, placeOfP);
    
#ifdef debug
    dout << "=====================================\n";
    rep(i,0,Q) {
        disP(P[i]); dout << " : ";
        rep(j,0,placeOfP[i].size()) {
            disP(placeOfP[i][j]);
        }
        dout << endl;
    }
    dout << "=====================================\n";
#endif
    
    rep(i,0,Q) {
        printf("%d\n", placeOfP[i].size()!=0);
    }
    
    

    
    
    //------------------------------------------------------------------------------------------
#ifdef debug
    //stop timer
    auto endTime = chrono::system_clock::now();
    auto dur = endTime - startTime;
    auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
    dout << fixed << setprecision(4) << (double)msec/1000 << " sec \n";
#endif
    //------------------------------------------------------------------------------------------
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

Compiler output:
a.cc: In function 'void display_DFS(PMA*)':
a.cc:186:13: error: 'struct PMA' has no member named 'str'
  186 |     disp(r->str);
      |             ^~~
a.cc:45:52: note: in definition of macro 'disp'
   45 | #define disp(A) dout << #A << " = " << setw(3) << (A) << endl
      |                                                    ^