#include #define ll long long #define pb push_back #define ff first #define ss second #define mp make_pair #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() using namespace std; const char nl='\n'; const int MOD=1000000007; const int N=1000005; int n;//, niz[N]; //bool mog[1000005]; struct TrieNode{ vector children; char c; int words = 0; TrieNode() {} TrieNode(char _c) : c(_c) {} }; struct TrieNodeBoth{ vector children; char c1, c2; int words = 0; TrieNodeBoth() {} TrieNodeBoth(char _c1, char _c2) : c1(_c1), c2(_c2) {} }; TrieNode *PrefixTrie = new TrieNode(); TrieNode *SuffixTrie = new TrieNode(); TrieNodeBoth *BothTrie = new TrieNodeBoth(); void print_Trie(TrieNode* node, int spacing); void print_Trie_both(TrieNodeBoth* node, int spacing); int tr = 0; void solve(){ cin>>n; string rec; for(int i=1;i<=n;i++) { cin>>rec; TrieNode* current = PrefixTrie; /*cout<c; { TrieNode* currentt = PrefixTrie; currentt->c = 'b'; TrieNode* nodeA = new TrieNode('g'); currentt->children.push_back(*nodeA); currentt = ¤tt->children.back(); { TrieNode* nodeg = new TrieNode('h'); currentt->children.push_back(*nodeg); } } cout<children[0].children[0].c;*/ for (int j=0;jchildren) { if (t.c == rec[j]) { current = &t; current->words++; found = true; break; } } if (!found) { //cout<c; TrieNode node = TrieNode(rec[j]); TrieNode *newNode = &node; current->children.push_back(*newNode); //cout<children.size(); current = ¤t->children.back(); //cout<children.size(); current->words++; //cout<<" (" << current->c << "," << current->words << ") "<c << ") "<=0;j--) { bool found = false; for (auto &t : current->children) { if (t.c == rec[j]) { current = &t; current->words++; found = true; break; } } if (!found) { //cout<c; TrieNode node = TrieNode(rec[j]); TrieNode *newNode = &node; //TrieNode *newNode = new TrieNode(rec[j]); current->children.push_back(*newNode); //cout<children.size(); current = ¤t->children.back(); //cout<children.size(); current->words++; //cout<< ", " << current->c << ") "<children) { if (t.c1 == rec[j] && t.c2 == rec[rec.size()-j-1]) { currentBoth = &t; currentBoth->words++; found = true; break; } } if (!found) { //cout<c; TrieNodeBoth node = TrieNodeBoth(rec[j], rec[rec.size()-j-1]); TrieNodeBoth *newNode = &node; currentBoth->children.push_back(*newNode); //cout<children.size(); currentBoth = ¤tBoth->children.back(); //cout<children.size(); currentBoth->words++; //cout<<" (" << current->c << "," << current->words << ") "<c << ") "<>q; while(q--) { cin>>name; int sum = 0; TrieNode* current = PrefixTrie; bool found = false; for (int j=0;jchildren) { if (t.c == name[j]) { current = &t; found = true; break; } } if (!found) { break; } } if (found) { sum+=current->words; } //cout<=0;j--) { found = false; for (auto &t : current->children) { if (t.c == name[j]) { current = &t; found = true; break; } } if (!found) { break; } } //cout<words; } TrieNodeBoth* currentBoth = BothTrie; for (int j=0;jchildren) { // cout<<"Lc1 "<words; } cout<c<<" "<words<<" "<children.size()<children) { print_Trie(&t, spacing+1); } } void print_Trie_both(TrieNodeBoth* node, int spacing) { for(int i=0;i<=spacing;i++) cout<<" "; cout<c1<<" "<c2<<" "<words<<" "<children.size()<children) { print_Trie_both(&t, spacing+1); } } int main () { cin.tie(NULL); ios_base::sync_with_stdio(false); freopen("wordstone.in", "r", stdin); freopen("wordstone.out", "w", stdout); int T=1; while(T--) { solve(); } return 0; }