[后缀数组 后缀树] Codechef January Challenge 2018 #KILLKTH Killjee and k-th letter

版权声明:本文为博主原创文章,未经博主允许随意转载。 https://blog.csdn.net/u014609452/article/details/79106100

建出后缀树,记录每个子串的出现次数,然后二分下答案在哪个子串中就好了

退役选手不会写后缀自动机

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define pb push_back
using namespace std;
typedef long long ll;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}
inline void read(ll &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}
inline int read(char *s){
  char c=nc(); int len=0;
  for (;!(c>='a' && c<='z');c=nc());
  for (;c>='a' && c<='z';s[++len]=c,c=nc()); s[++len]=0; return len-1;
}

const int N=400005;

int n; char S[N];

int sa[N],t1[N],t2[N],c[N];  
int height[N],rnk[N];  


inline void SA(char *r,int m){  
  int *x=t1,*y=t2;  
  for (int i=0;i<=m;i++) c[i]=0;  
  for (int i=1;i<=n;i++) c[x[i]=r[i]-'a'+1]++;  
  for (int i=1;i<=m;i++) c[i]+=c[i-1];  
  for (int i=n;i;i--) sa[c[x[i]]--]=i;  
  for (int k=1;k<=n;k<<=1){  
    int p=0;  
    for (int i=n-k+1;i<=n;i++) y[++p]=i;  
    for (int i=1;i<=n;i++) if (sa[i]>k) y[++p]=sa[i]-k;  
    for (int i=0;i<=m;i++) c[i]=0;  
    for (int i=1;i<=n;i++) c[x[y[i]]]++;  
    for (int i=1;i<=m;i++) c[i]+=c[i-1];  
    for (int i=n;i;i--) sa[c[x[y[i]]]--]=y[i];  
    swap(x,y);  
    x[sa[1]]=1; p=1;  
    for (int i=2;i<=n;i++)  
      x[sa[i]]=(y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k])?p:++p;  
    if (p>=n) break;  
    m=p;  
  }  
}   
inline void GetHeight(){  
  int j,k=0;  
  for (int i=1;i<=n;i++) rnk[sa[i]]=i;  
  for (int i=1;i<=n;height[rnk[i++]]=k)  
    for (k?k--:0,j=sa[rnk[i]-1];S[i+k]==S[j+k];)  
      k++;  
}

#define V G[p].v

struct edge{
  int u,v,next;
}G[N];
int head[N],inum;
inline void add(int u,int v,int p){
  G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}

int depth[N],ed[N],cnt[N];
int fat[N];

int tot;
int sta[N],pnt;
inline void Build(){
  sta[++pnt]=++tot; sta[++pnt]=++tot;
  depth[tot]=n-sa[1]+1; ed[tot]=n; cnt[tot]++;
  for (int i=2;i<=n;i++){
    int h=height[i],last=0;
    while (depth[sta[pnt]]>h){
      if (last) add(sta[pnt],last,++inum);
      last=sta[pnt--];
    }
    if (depth[sta[pnt]]<h){
      sta[++pnt]=++tot;
      depth[tot]=h;
      ed[tot]=ed[last]-(depth[last]-h);
    }
    if (last) add(sta[pnt],last,++inum);
    if (n-sa[i]+1==h)
      cnt[tot]++;
    else{
      sta[++pnt]=++tot;
      depth[tot]=n-sa[i]+1;
      ed[tot]=n;
      cnt[tot]=1;
    }
  }
  while (pnt>1) add(sta[pnt-1],sta[pnt],++inum),pnt--;
}
int lst[N];
ll Cnt[N];
inline void dfs(int u,int fa){
  lst[++*lst]=u; fat[u]=fa;
  vector<int> v;
  for (int p=head[u];p;p=G[p].next)
    v.pb(V);
  for (int i=v.size()-1;~i;i--)
    if (v[i]!=fa)
      dfs(v[i],u),cnt[u]+=cnt[v[i]];
}

inline ll SS(int l,int r){
  return (ll)(l+r)*(r-l+1)/2;
}

int main(){
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  n=read(S); SA(S,26); GetHeight();
  Build();
  dfs(1,0);
  for (int i=2;i<=tot;i++)
    Cnt[i]=cnt[lst[i]]*(SS(depth[fat[lst[i]]]+1,depth[lst[i]]))+Cnt[i-1];
  int Q; read(Q); ll G=0,P,M,K;
  while (Q--){
    read(P); read(M); K=P*G%M+1;
    int it=lower_bound(Cnt+2,Cnt+tot+1,K)-Cnt,p=lst[it];
    K-=Cnt[it-1];
    int L=depth[fat[p]],R=depth[p],C=cnt[p],MID;
    while (L+1<R)
      if (K>SS(depth[fat[p]]+1,MID=(L+R)>>1)*C)
    L=MID;
      else
    R=MID;
    K-=SS(depth[fat[p]]+1,R-1)*C;
    K=(K-1)%R+1;
    char ans=S[ed[p]-depth[p]+K];
    G+=(int)ans;
    putchar(ans); putchar('\n');
  }
  return 0;
}
阅读更多

没有更多推荐了,返回首页