Language: C++

#include<stdio.h>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
struct NODE{
    int lid ,llen;
    int rid, rlen;
    string rem;
    NODE(){}
    NODE(int _l,int _ll ,int _r,int _rl,string &s){
        lid = _l;llen = _ll;rid =_r;rlen = _rl;rem = s;
    }
};
bool operator<(const NODE &a,const NODE &b){
    if(a.lid != b.lid) return a.lid < b.lid;
    if(a.rid != b.rid) return a.rid < b.rid;
    if(a.llen != b.llen) return a.llen < b.llen;
    if(a.rlen != b.rlen) return a.rlen < b.rlen;
    return a.rem < b.rem;
}
map<NODE,int> hash;
map<NODE,int> fin;
int conn[101][101];
int n;
string str[101] , rev[101];
bool judge(string &s ,string &t){
    int r = min(t.size() , s.size());
    for(int i = 0 ,ps = 0 , pt = t.size()-1 ; i < r;i++,ps++,pt--)
        if(s[ps] != t[pt])
            return false;
    return true;
}
bool end(NODE &s){
    if(conn[s.lid][s.rid] == false) return false;
    for(int i = 0; i <s.rem.size()/2 ;i++)
        if(s.rem[i] != s.rem[s.rem.size()-i-1])
            return false;
    return true;
}
int dfs(NODE s,int dep){
    fin[s]++;
    //for(int i= 0; i< dep*2;i++)
    //    putchar('-');
    //printf("l = %d ll = %d r= %d rl = %d %s\n",s.lid,s.llen,s.rid,s.rlen,s.rem.c_str());
    if(hash.count(s) > 0 && fin[s] == 2) {
        if(hash[s] >= 0) {if(fin[s] !=0)return -2; return hash[s];}
        else{
            if(end(s)) return hash[s] = -2;
            return hash[s];
        }
    }
    hash[s] = -1;
    int ret = -1;
    if(end(s)){
        hash[s] =ret = s.rem.size();
    //    puts("ok");
    }

    if(s.llen == 0){
        for(int i = 0 ; i< n ;i++){
            if(conn[s.lid][i] && judge(str[i] , s.rem)){
                if(str[i].size() < s.rem.size()){
                    int nlen = s.rem.size() - str[i].size();
                    string nsub = s.rem.substr(0,nlen);
                    NODE ns = NODE(i , 0 , s.rid ,  nlen , nsub);
                    int ntmp = dfs(ns,dep+1);// + str[i].size();
                    if(ntmp == -2){
                        fin[s]--;
                        return hash[s] = -2;
                    }else if(ntmp == -1) continue;
                    else {
                        ret = max(ret, ntmp + (int)str[i].size()*2);
                    }
                }else{
                    int nlen = str[i].size() - s.rem.size();
                    string nsub = str[i].substr(s.rem.size());
                    NODE ns = NODE(i , nlen , s.rid , 0 ,nsub);
                    int ntmp = dfs(ns,dep+1);
                    if(ntmp == -2){
                        fin[s]--;
                        return hash[s] = -2;
                    }else if(ntmp == -1) continue;
                    else ret = max(ret , ntmp + (int)s.rem.size()*2);
                }
            }
        }
    }else{
        for(int i = 0 ; i< n ;i++){
            if(conn[i][s.rid] && judge(s.rem,rev[i])){
                //printf("jiong");
                if(rev[i].size() < s.rem.size()){
                    int nlen = s.rem.size() - rev[i].size();
                    string nsub = s.rem.substr(rev[i].size());
                    NODE ns = NODE(s.lid , nlen , i ,  0 , nsub);
                    int ntmp = dfs(ns,dep+1);// + str[i].size();
                    if(ntmp == -2){
                        fin[s]--;
                        return hash[s] = -2;
                    }else if(ntmp == -1) continue;
                    else ret = max(ret, ntmp + (int)rev[i].size()*2);
                }else{
                    int nlen = rev[i].size() - s.rem.size();
                    string nsub = rev[i].substr(0,nlen);
                    NODE ns = NODE(s.lid ,0  , i , nlen ,nsub);
                    int ntmp = dfs(ns,dep+1);
                    if(ntmp == -2){
                        return hash[s] = -2;
                    }else if(ntmp == -1) continue;
                    else ret = max(ret , ntmp + (int)s.rem.size()*2);
                }
            }
        }
    }
    //for(int i= 0; i< dep*2;i++)
    //    putchar('-');
    //printf("l = %d ll = %d r= %d rl = %d %s %d\n",s.lid,s.llen,s.rid,s.rlen,s.rem.c_str(),ret);
    fin[s]--;
    return hash[s] = ret;
}
char ts[1000];
int main(){
    //string sa = "ab" ,sb = "ba";
    //printf("%d\n",judge(sa,sb));
    while(scanf("%d%d",&n,&m) == 2){
    memset(conn,0,sizeof(conn));
    fin.clear();
    hash.clear();
    int m; ;
    for(int i = 0; i < n; i++){
        scanf("%s",ts);
        str[i] = ts;
        //reverse(ts,ts+strlen(ts));
        rev[i] = ts;
    }
    str[n] = "!";
    rev[n] = "!";
    for(int i = 0; i< n ;i++)
        conn[i][n] = conn[n][i] = 1;
    for(int i = 0; i < m;i++){
        int a,b;scanf("%d%d",&a,&b);
        --a;--b;
        conn[a][b] = 1;
    }
    string em = "";
    NODE start = NODE(n,0,n,0,em);
    int ret = dfs(start,0);
    if(ret == -2)
        puts("-1");
    else if(ret == -1)
        puts("0");
    else printf("%d\n",ret);
    }
    return 0;
}

Compiler output:
a.cc: In function 'int dfs(NODE, int)':
a.cc:48:8: error: reference to 'hash' is ambiguous
   48 |     if(hash.count(s) > 0 && fin[s] == 2) {
      |        ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from a.cc:2:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:49:12: error: reference to 'hash' is ambiguous
   49 |         if(hash[s] >= 0) {if(fin[s] !=0)return -2; return hash[s];}
      |            ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:49:59: error: reference to 'hash' is ambiguous
   49 |         if(hash[s] >= 0) {if(fin[s] !=0)return -2; return hash[s];}
      |                                                           ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:51:31: error: reference to 'hash' is ambiguous
   51 |             if(end(s)) return hash[s] = -2;
      |                               ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:52:20: error: reference to 'hash' is ambiguous
   52 |             return hash[s];
      |                    ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:55:5: error: reference to 'hash' is ambiguous
   55 |     hash[s] = -1;
      |     ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:58:9: error: reference to 'hash' is ambiguous
   58 |         hash[s] =ret = s.rem.size();
      |         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:72:32: error: reference to 'hash' is ambiguous
   72 |                         return hash[s] = -2;
      |                                ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:84:32: error: reference to 'hash' is ambiguous
   84 |                         return hash[s] = -2;
      |                                ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:101:32: error: reference to 'hash' is ambiguous
  101 |                         return hash[s] = -2;
      |                                ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:110:32: error: reference to 'hash' is ambiguous
  110 |                         return hash[s] = -2;
      |                                ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc:121:12: error: reference to 'hash' is ambiguous
  121 |     return hash[s] = ret;
      |            ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~
a.cc: In function 'int main()':
a.cc:127:28: error: 'm' was not declared in this scope; did you mean 'tm'?
  127 |     while(scanf("%d%d",&n,&m) == 2){
      |                            ^
      |                            tm
a.cc:130:5: error: reference to 'hash' is ambiguous
  130 |     hash.clear();
      |     ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:24:15: note:                 'std::map<NODE, int> hash'
   24 | map<NODE,int> hash;
      |               ^~~~