#include #include #include #include using namespace std; struct node{ node* e[27]; int cnt; node(){ cnt = 0; fill(e,e+27, nullptr); } }; class trie_tree{ node* root; public: trie_tree(){ root = new node(); root->cnt = 1; } int insert(node* ptr, string& s, int pos){ if(pos == s.size()) return 0; ptr->cnt++; if(ptr->e[s[pos]-'a'] == nullptr){ ptr->e[s[pos]-'a'] = new node(); } return insert(ptr->e[s[pos]-'a'], s, pos+1) + (ptr->cnt>1 ? 1 : 0); } int insert(string& s){ return insert(root, s, 0); } }; int main(){ int n; cin >> n; vector s(n); for(int i=0; i> s[i]; s[i] += '{'; } vector x(n); for(int i=0; i> x[i]; x[i]--; } trie_tree trie; vector ans(n); for(int i=n-1; i>=0; i--){ ans[i] = trie.insert( s[x[i]] ); } for(int i=0; i