#include #include #include #include struct Trie { Trie() : nchildren(0) { for(int i=0; i<26; i++) children[i] = NULL; } ~Trie() { //for(int i=0; i<26; i++) // delete children[i]; // Stack overflows! } int nchildren; Trie *children[26]; }; void insert(Trie *t, const std::string &s) { int L = s.length(); for(int pos=0; posnchildren++; int idx = s[pos]-'a'; if(!t->children[idx]) t->children[idx] = new Trie(); t = t->children[idx]; } t->nchildren++; } int query(Trie *t, const std::string &s) { int L = s.length(); for(int pos=0; poschildren[idx]) return 0; t = t->children[idx]; } return t->nchildren; } std::string alternate(const std::string &s) { int L = s.length(); std::string result; result.resize(2*L); for(int i=0; i> n >> q; Trie prefixT; Trie suffixT; Trie bothT; for(int i=0; i> s; insert(&prefixT, s); std::string r(s.rbegin(), s.rend()); insert(&suffixT, r); insert(&bothT, alternate(s)); } for(int i=0; i> command >> prefix >> suffix; std::reverse(suffix.begin(), suffix.end()); int L = prefix.length(); std::string both; both.resize(2*L); for(int j=0; j