The subject is described now to do the problem ... To me is kpm ... First Orz the Cloud God ~
By creating a trie of all the strings, and then establishing the DFS sequence, the subtree of each point on the trie corresponds to the number of periods on the DFS sequence.
Then insert the label of each string, without modification, only the Chairman tree is required.
#include <cstdlib> #include <cstdio> #include <cctype> #include <algorithm> #include < cstring> #include <cmath> #include <iostream> #include <queue> #define REP (i, L, R) for (int i=l; i< =r; i++) #define DOWN (i, L, R) for (int i=l; i>=r; i--) #define CLR (x, C) memset (x, C, sizeof (x)) #define T (x) tree[x] #define MAXN 100009#define Maxl 200009using namespace Std;inline int read () {int x=0; char Ch=getchar (); while (!isdigit (CH)) Ch=get char (); while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar (); return x;} struct node{int s; node *l, *r;} *blank=new (node), *tree[maxl];struct sr{int A, b;} q[maxn];int N, Z, now, t[maxl][26], l[ MAXL], R[maxl], W[maxn];char s[maxl];inline BOOL CMP (SR A, SR B) {return L[A.A]<L[B.A];} void Add (int k, int l, int r, node *u, node*&t) {if (T==blank) t=new (node), T->l=t->r=blank, t->s=0;t->s=u- >s+1;if (l==r) return; int mid= (L+R) >>1;if (k<=mid) T->r=u->r, Add (K, L, Mid, u->l, t->l); else T-> L=u->l, Add (K, mid+1, R, U->r, T->r);} inline int Query (int l, int r, int k) {if (T (r)->s-t (L)->s<k) return-1; K--;int l=1, r=n; node *u=t (L), *v=t (R); while (l<r) {int mid= (l+r) >>1, Sum=v->l->s-u->l->s;if (sum<=k) l=mid+1, V=v->r, U=u->r, K-=sum;else R=mid, V=v->l, U=u->l;} return L;} void Dfs (int x) {L[x]=++now; Rep (i, 0, +) if (T[x][i]) DFS (t[x][i]); r[x]=now;} inline void Init () {Blank->l=blank->r=blank, blank->s=0; t (0) =new (node), T (0)->l=t (0)->r=blank, T (0)- >s=0;} int main () {n=read (); Init (); Rep (i, 1, n) {scanf ("%s", s), int len=strlen (s), Now=0;down (J, len-1, 0) t[now][s[j]-' A ']==0? now=t[now][s[j]-' a '] =++z:now=t[now][s[j]-' A '];q[i].a=now, q[i].b=i, W[i]=now;} now=0; DFS (0); Sort (q+1, q+1+n, CMP); for (int i=1, k=1; k<=z+1; k++) {node *p=t (k-1); while (l[q[i].a]==k) Add (q[i++].b, 1, N, p, t (k) =BL Ank), p=t (k); t (k) =p;} Rep (i, 1, N) printf ("%d\n", Query (L[w[i]]-1, R[w[i]], read ()));}
BZOJ-3439 KPM's MC password