Language: C++

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <cctype>
#include <cstring>
#include <sstream>
#define MOD 1000000007LL

using namespace std;

typedef long long ll;
typedef string::const_iterator State;
typedef pair<int,int> P;
int n[4];
int m;
string str[51];
map<string,int> index;
vector<P> vec[51];
vector<int> rule;
ll dp[2][51][51][51][51];

void make_rule(int v){
	for(int i=0;i<vec[v].size();i++){
		if(vec[v][i].first==0){
			rule.push_back(vec[v][i].second);
		}else{
			make_rule(vec[v][i].second);
		}
	}
}

void parse_2(State &begin,State &end,int par){
	if(*begin==' ')begin++;
	if(begin==end)return;
	if(*begin=='['){
		begin++;
		int bit=0;
		while(*begin!=']'){
			if(*begin=='A')bit|=(1<<0);
			if(*begin=='T')bit|=(1<<1);
			if(*begin=='G')bit|=(1<<2);
			if(*begin=='C')bit|=(1<<3);
			begin++;
		}
		begin++;
		vec[par].push_back(P(0,bit));
	}else{
		string st="";
		while(*begin!=' ' && begin!=end){
			st+=*begin;
			begin++;
		}
		if(index.find(st)==index.end()){
			int next=index.size();
			index[st]=next;
		}
		vec[par].push_back(P(1,index[st]));
	}
	parse_2(begin,end,par);
	return;
}

void parse_1(State &begin,State &end){
	string st="";
	while(*begin!=':'){
		st+=*begin;
		begin++;
	}
	if(index.find(st)==index.end()){
		int next=index.size();
		index[st]=next;
	}
	int par=index[st];
	begin++;
	begin++;
	parse_2(begin,end,par);
	return;
}

int main(void){
	for(int i=0;i<4;i++){
		scanf("%d",&n[i]);
	}
	scanf("%d%*c",&m);

	for(int i=0;i<m;i++){
		getline(cin,str[i]);
		State begin=str[i].begin();
		State end=str[i].end();
		parse_1(begin,end);
	}
	make_rule(0);
	int prev=0,now=1;
	dp[prev][0][0][0][0]=1;
	for(int i=0;i<rule.size();i++){
		int g[4];
		for(g[0]=0;g[0]<=n[0];g[0]++){
			for(g[1]=0;g[1]<=n[1];g[1]++){
				for(g[2]=0;g[2]<=n[2];g[2]++){
					for(g[3]=0;g[3]<=n[3];g[3]++){
						if(dp[prev][g[0]][g[1]][g[2]][g[3]]>=1){
							if((rule[i]>>0 & 1) && g[0]+1<=n[0]){
								dp[now][g[0]+1][g[1]][g[2]][g[3]]=(dp[now][g[0]+1][g[1]][g[2]][g[3]]+dp[prev][g[0]][g[1]][g[2]][g[3]])%MOD;
							}
							if((rule[i]>>1 & 1) && g[1]+1<=n[1]){
								dp[now][g[0]][g[1]+1][g[2]][g[3]]=(dp[now][g[0]][g[1]+1][g[2]][g[3]]+dp[prev][g[0]][g[1]][g[2]][g[3]])%MOD;
							}
							if((rule[i]>>2 & 1) && g[2]+1<=n[2]){
								dp[now][g[0]][g[1]][g[2]+1][g[3]]=(dp[now][g[0]][g[1]][g[2]+1][g[3]]+dp[prev][g[0]][g[1]][g[2]][g[3]])%MOD;
							}
							if((rule[i]>>3 & 1) && g[3]+1<=n[3]){
								dp[now][g[0]][g[1]][g[2]][g[3]+1]=(dp[now][g[0]][g[1]][g[2]][g[3]+1]+dp[prev][g[0]][g[1]][g[2]][g[3]])%MOD;
							}
						}
					}
				}
			}
		}
		swap(now,prev);
		memset(dp[now],0,sizeof(dp[now]));
	}
	printf("%lld\n",dp[prev][n[0]][n[1]][n[2]][n[3]]);
	return 0;
}

Compiler output:
a.cc:19:17: error: 'std::map<std::__cxx11::basic_string<char>, int> index' redeclared as different kind of entity
   19 | map<string,int> index;
      |                 ^~~~~
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from a.cc:7:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'void parse_2(State&, State&, int)':
a.cc:55:26: error: overloaded function with no contextual type information
   55 |                 if(index.find(st)==index.end()){
      |                          ^~~~
a.cc:55:42: error: overloaded function with no contextual type information
   55 |                 if(index.find(st)==index.end()){
      |                                          ^~~
a.cc:56:40: error: overloaded function with no contextual type information
   56 |                         int next=index.size();
      |                                        ^~~~
a.cc:57:30: error: no match for 'operator[]' (operand types are '<unresolved overloaded function type>' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
   57 |                         index[st]=next;
      |                              ^
a.cc:59:45: error: no match for 'operator[]' (operand types are '<unresolved overloaded function type>' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
   59 |                 vec[par].push_back(P(1,index[st]));
      |                                             ^
a.cc: In function 'void parse_1(State&, State&)':
a.cc:71:18: error: overloaded function with no contextual type information
   71 |         if(index.find(st)==index.end()){
      |                  ^~~~
a.cc:71:34: error: overloaded function with no contextual type information
   71 |         if(index.find(st)==index.end()){
      |                                  ^~~
a.cc:72:32: error: overloaded function with no contextual type information
   72 |                 int next=index.size();
      |                                ^~~~
a.cc:73:22: error: no match for 'operator[]' (operand types are '<unresolved overloaded function type>' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
   73 |                 index[st]=next;
      |                      ^
a.cc:75:22: error: no match for 'operator[]' (operand types are '<unresolved overloaded function type>' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
   75 |         int par=index[st];
      |                      ^