BZOJ3439: Kpm的MC密码

Description


 背景

    想Kpm当年为了防止别人随便进入他的MC,给他的PC设了各种奇怪的密码和验证问题(不要问我他是怎么设的。。。),于是乎,他现在理所当然地忘记了密码,只能来解答那些神奇的身份验证问题了。。。

 描述

    Kpm当年设下的问题是这样的:

    现在定义这么一个概念,如果字符串s是字符串c的一个后缀,那么我们称c是s的一个kpm串。

    系统将随机生成n个由a…z组成的字符串,由1…n编号(s1,s2…,sn),然后将它们按序告诉你,接下来会给你n个数字,分别为k1…kn,对于每一个ki,要求你求出列出的n个字符串中所有是si的kpm串的字符串的编号中第ki小的数,如果不存在第ki小的数,则用-1代替。(比如说给出的字符串是cd,abcd,bcd,此时k1=2,那么”cd”的kpm串有”cd”,”abcd”,”bcd”,编号分别为1,2,3其中第2小的编号就是2)(PS:如果你能在相当快的时间里回答完所有n个ki的查询,那么你就可以成功帮kpm进入MC啦~~)

Input

 

    第一行一个整数 n 表示字符串的数目

    接下来第二行到n+1行总共n行,每行包括一个字符串,第i+1行的字符串表示编号为i的字符串

    接下来包括n行,每行包括一个整数ki,意义如上题所示

 

Output

 

    包括n行,第i行包括一个整数,表示所有是si的kpm串的字符串的编号中第ki小的数

 

Sample Input


3
cd
abcd
bcd
2
3
1

Sample Output

2
-1
2

样例解释

“cd”的kpm 串有”cd”,”abcd”,”bcd”,编号为1,2,3,第2小的编号是

2,”abcd”的kpm串只有一个,所以第3小的编号不存在,”bcd”的kpm

串有”abcd”,”bcd”,第1小的编号就是2。

数据范围与约定

设所有字符串的总长度为len


对于100%的数据,1<=n<=100000,0<len<=300000

 

建出AC自动机,得到last指针,连成树后转化成子树k小问题,写个主席树。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
    if(head==tail) {
        int l=fread(buffer,1,BufferSize,stdin);
        tail=(head=buffer)+l;
    }
    return *head++;
}
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
const int maxn=100010;
const int maxnode=300010;
int first[maxnode],next[maxnode],val[maxnode],Pos[maxn],tot;
int ch[maxnode][26],cnt;
void insert(char* s,int v) {
    int j=0,c;
    for(int i=0;s[i];i++) {
        c=s[i]-'a';
        if(!ch[j][c]) ch[j][c]=++cnt;
        j=ch[j][c];
    }
    val[++tot]=v;Pos[v]=j;next[tot]=first[j];first[j]=tot;
}
int q[maxnode],f[maxnode],last[maxnode];
int First[maxnode],Next[maxnode],to[maxnode],e;
void AddEdge(int u,int v) {
    to[++e]=v;Next[e]=First[u];First[u]=e;
}
void getfail() {
    int l=1,r=0;
    rep(c,0,25) if(ch[0][c]) q[++r]=ch[0][c];
    while(l<=r) {
        int x=q[l++];
        rep(c,0,25) if(ch[x][c]) {
            int u=ch[x][c],j=f[x];q[++r]=u;
            while(j&&!ch[j][c]) j=f[j];
            f[u]=ch[j][c];last[u]=first[f[u]]?f[u]:last[f[u]];
        }
    }
    rep(i,1,cnt) if(first[i]) AddEdge(last[i],i);
}
char s[maxnode];
int ls[maxn*20],rs[maxn*20],siz[maxn*20],ToT,L[maxnode],R[maxnode];
int root[maxnode],cur;
void build(int& y,int x,int l,int r,int pos) {
    siz[y=++ToT]=siz[x]+1;if(l==r) return;
    int mid=l+r>>1;ls[y]=ls[x];rs[y]=rs[x];
    if(pos<=mid) build(ls[y],ls[x],l,mid,pos);
    else build(rs[y],rs[x],mid+1,r,pos);
}
int query(int y,int x,int l,int r,int k) {
    if(siz[y]-siz[x]<k) return -1;
    if(l==r) return l;
    int mid=l+r>>1,k2=siz[ls[y]]-siz[ls[x]];
    if(k<=k2) return query(ls[y],ls[x],l,mid,k);
    return query(rs[y],rs[x],mid+1,r,k-k2);
}
void dfs(int x) {
    for(int i=first[x];i;i=next[i]) {
        cur++;if(!L[x]) L[x]=cur;
        build(root[cur],root[cur-1],1,100000,val[i]);
    }
    for(int i=First[x];i;i=Next[i]) dfs(to[i]);
    R[x]=cur;
}
int main() {
    int n=read();
    rep(i,1,n) scanf("%s",s),insert(s,i);
    getfail();dfs(0);
    rep(i,1,n) printf("%d\n",query(root[R[Pos[i]]],root[L[Pos[i]]-1],1,100000,read()));
    return 0;
}
View Code

 

posted @ 2015-11-25 18:56  wzj_is_a_juruo  阅读(216)  评论(0编辑  收藏  举报