Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cassert>

#define INF (1<<29)

using namespace std;

const string ds="URDL";
const int dx[4]={0,1,0,-1};
const int dy[4]={-1,0,1,0};
int n,m;
vector<string>g;

struct AhoCorasick{
  static const int max_n =101;
  static const int alph_size=26;

  struct node_t{
    int parent,suffLink;
    char charFromParent;
    int children[alph_size];
    int transitions[alph_size];
    bool isLeaf;

    node_t(){
      fill(children,children+alph_size,-1);
      fill(transitions,transitions+alph_size,-1);
      suffLink=-1;
      isLeaf=false;
    }
  };

  node_t *nodes[max_n];
  int nodeCount;
  
  AhoCorasick(){
    nodes[0]=new node_t();
    nodes[0]->suffLink=0;
    nodes[0]->parent=-1;
    nodeCount=1;
    fill(memo[0],memo[max_n],-2);
  }

  void add(string s){
    int cur=0;
    for(int i=0;i<s.size();i++){
      int c=s[i]-'A';
      if(nodes[cur]->children[c]==-1){
	nodes[nodeCount]=new node_t();
	nodes[nodeCount]->parent=cur;
	nodes[nodeCount]->charFromParent=s[i];
	nodes[cur]->children[c]=nodeCount++;
      }
      cur=nodes[cur]->children[c];
    }
    nodes[cur]->isLeaf=true;
  }  

  int suffLink(int nodeIndex){
    node_t *node=nodes[nodeIndex];
    if(node->suffLink==-1){
      node->suffLink=(node->parent==0)?0:
	transition(suffLink(node->parent),node->charFromParent);
    }
    return node->suffLink;  
  }

  int transition(int nodeIndex,char ch){
    int c=ch-'A';
    node_t *node=nodes[nodeIndex];
    
    if(node->transitions[c]==-1){
      node->transitions[c]=(node->children[c]!=-1)?node->children[c]:
	(nodeIndex==0?0:transition(suffLink(nodeIndex),ch));
    }
    return node->transitions[c];
  }

  int contain(string s){
    int node=0,cnt=0;
    for(int i=0;i<s.size();i++){
      node=transition(node,s[i]);
      cnt+=nodes[node]->isLeaf;
    }
    return cnt;
  }
};

struct state{int step,x,y,nodeIndex;};

int dist[51][51][101];

int bfs(){
  
  int p;
  cin >> p;
  
  AhoCorasick aho;
  
  for(int i=0;i<p;i++){
    string s;
    cin >> s;
    aho.add(s);
  }

  fill(*dist[0],*dist[51],INF);
  queue<state>que;
  
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(g[i][j]=='S'){
	dist[i][j][0]=0;
	que.push((state){0,j,i,0});
	break;
      }
    }
  }
  
  while(!que.empty()){
    state now=que.front();
    que.pop();

    if(g[now.y][now.x]=='G')return now.step;
    if(dist[now.y][now.x][now.nodeIndex]<now.step)continue;
    dist[now.y][now.x][now.nodeIndex]=now.step;
    
    for(int i=0;i<4;i++){
      int nx=now.x+dx[i],ny=now.y+dy[i];
      if(nx<0 || ny<0 || m<=nx || n<=ny)continue;
      if(g[ny][nx]=='#')continue;

      int nextNode=aho.transition(now.nodeIndex,ds[i]);      
      if(dist[ny][nx][nextNode]<now.step+1)continue;
      if(aho.nodes[nextNode]->isLeaf)continue;
      
      que.push((state){now.step+1,nx,ny,nextNode});
    }
  }
  return -1;
}

int main(void){

  while(cin >> n >> m,n|m){
    g.resize(n);
    for(int i=0;i<n;i++)cin >> g[i];

    cout << bfs() << endl;
  }

  return 0;
}

Compiler output:
a.cc: In constructor 'AhoCorasick::AhoCorasick()':
a.cc:45:10: error: 'memo' was not declared in this scope
   45 |     fill(memo[0],memo[max_n],-2);
      |          ^~~~