#include #include #include using namespace std; struct node; struct node { node* children[26]; node* autocomplete; int d; node() : autocomplete(NULL), d(0) { memset(children,0,sizeof(children)); } }; pair insert(node*& x, const char* c) { if (x==NULL) x = new node; char cc=*c; if (cc==0) { if (x->autocomplete==NULL) x->autocomplete=x; return {1,x}; } cc-='a'; auto da = insert(x->children[cc],c+1); if (x->autocomplete==NULL) tie(x->d, x->autocomplete) = da; return {x->d+1, x->autocomplete}; } int search(const node* x, const char* c, int cost) { if (x==NULL) return strlen(c); char cc=*c; //cerr << cc << " at " << x << endl; node* y = NULL; if (cc!=0) y = x->children[cc-'a']; //cerr << "aut " << (x->autocomplete) << endl; if (y==NULL or y->autocomplete != x->autocomplete) { int alt=1+x->d; //cerr << "STD " << cost << " TAB " << alt << endl; if (alt> n >> m; node* t = NULL; for (int i=0;i> s; insert(t,s.c_str()); } t->autocomplete=t; for (int i=0;i> s; cout << search(t, s.c_str(), 0) << endl; } }