zoukankan      html  css  js  c++  java
  • 【bzoj3881】【Coci2015】Divljak

    •  题解

      • 对$S$集合ac建自动机,把$T_{i}$放在里面跑,记录路径上的所有节点并对它们在fail树上求到root的树链并;
      • 这样就得到了$T_{i}$所有的子串;
      • 动态将$T_{i}$加入直接用树状数组维护子树和即可;
      • 另见:喵星球上的点名的ac自动机解法
      •   1 #include<bits/stdc++.h>
          2 #define rg register
          3 #define il inline 
          4 using namespace std;
          5 const int N=2000010;
          6 int n,m,cnt,ch[N][26],fl[N],fa[N],head,tail,que[N],o=1,hd[N],sz[N];
          7 int tp[N],dep[N],idx,st[N],ed[N],c[N],vis[N],id[N],tmp[N],tot,tt;
          8 char s[N];
          9 struct Edge{int v,nt;}E[N];
         10 il void adde(int u,int v){E[o]=(Edge){v,hd[u]};hd[u]=o++;}
         11 il char gc(){
         12     static char*p1,*p2,S[1000000];
         13     if(p1==p2)p2=(p1=S)+fread(S,1,1000000,stdin);
         14     return(p1==p2)?EOF:*p1++;
         15 }
         16 il int rd(){
         17     int x=0; char C=gc();
         18     while(C<'0'||C>'9')C=gc();
         19     while(C>='0'&&C<='9')x=(x<<1)+(x<<3)+C-'0',C=gc();
         20     return x;
         21 }
         22 il int gt(){
         23     char *p=s,C=gc();
         24     while(!isalpha(C))C=gc();
         25     while(isalpha(C))*p++=C,C=gc();
         26     return p - s;
         27 }
         28 il bool cmp(const int&a,const int&b){return st[a]<st[b];}
         29 il void ins(int now,int len){
         30     int u=0,x;
         31     for(int i=0;i<len;i++){
         32         if(!ch[u][x=s[i]-'a'])ch[u][x]=++cnt;
         33         u=ch[u][x];
         34     }
         35     sz[id[now]=u]=vis[u]=1;
         36 }
         37 void get_fl(){
         38     head=tail=0;
         39     for(rg int i=0;i<26;i++)if(ch[0][i]){
         40         que[++tail]=ch[0][i];
         41         if(vis[ch[0][i]])adde(0,ch[0][i]);
         42     }
         43     while(head<tail){
         44         int u=que[++head];
         45         for(rg int i=0;i<26;i++){
         46             int&v=ch[u][i];
         47             if(!v){v=ch[fl[u]][i];continue;}
         48             fl[v]=ch[fl[u]][i];
         49             if(vis[fl[v]])fa[v]=fl[v];
         50             else fa[v]=fa[fl[v]];
         51             if(vis[v])adde(fa[v],v);
         52             que[++tail]=v;
         53         }
         54     }
         55     for(int i=tail;i;i--)if(vis[que[i]])sz[fa[que[i]]]+=sz[que[i]];
         56 }
         57 il void dfs(int u,int T){
         58     st[u]=++idx;tp[u]=T;
         59     dep[u]=dep[fa[u]]+1;
         60     int mx=-1,son=0;
         61     for(rg int i=hd[u];i;i=E[i].nt){if(sz[E[i].v]>mx)mx=sz[son=E[i].v];}
         62     if(son)dfs(son,T); 
         63     for(rg int i=hd[u];i;i=E[i].nt){if(E[i].v!=son)dfs(E[i].v,E[i].v);}
         64     ed[u]=idx;
         65 }
         66 il void add(int x,int y){if(!x)return;for(;x<=idx;x+=x&-x)c[x]+=y;}
         67 il int ask(int x){int re=0;for(;x;x-=x&-x)re+=c[x];return re;}
         68 il int lca(int u,int v){
         69     int tu=tp[u],tv=tp[v];
         70     while(tu!=tv){
         71         if(dep[tu]<dep[tv])v=fa[tv],tv=tp[v];
         72         else u=fa[tu],tu=tp[u];
         73     }
         74     return dep[u]<dep[v]?u:v;
         75 }
         76 void update(int len){
         77     tt=tot=0;
         78     for(rg int i=0,u=0;i<len;i++){
         79         u=ch[u][s[i]-'a'];
         80         if(vis[u])tmp[++tt]=u;
         81         else if(fa[u])tmp[++tt]=fa[u];
         82     }
         83     if(!tt)return;
         84     sort(tmp+1,tmp+tt+1,cmp);
         85     tot=1;
         86     for(rg int i=2;i<=tt;i++){
         87         tot+=st[tmp[i]]>ed[tmp[tot]];
         88         tmp[tot]=tmp[i];
         89     }
         90     add(st[tmp[1]],1);
         91     for(rg int i=2;i<=tot;i++){
         92         add(st[lca(tmp[i],tmp[i-1])],-1);
         93         add(st[tmp[i]],1);
         94     }
         95 }
         96 int main(){
         97     #ifndef ONLINE_JUDGE
         98     freopen("bzoj3881.in","r",stdin);
         99     freopen("bzoj3881.out","w",stdout);
        100     #endif 
        101     n=rd();
        102     for(rg int i=1;i<=n;i++){ins(i,gt());}
        103     get_fl();
        104     dfs(0,0);
        105     m=rd();
        106     for(rg int i=1,x,y;i<=m;i++){
        107         x=rd();
        108         if(x==1)update(gt());
        109         else y=rd(),printf("%d
        ",ask(ed[id[y]])-ask(st[id[y]]-1));
        110     }
        111     return 0;
        112 }
        bzoj3881
  • 相关阅读:
    外部编辑Infopath的表单模板(xsn)
    安装SQL Server 2008时可能遇到性能计数器不一致的情况
    Windows应用程序中使用Windows验证方式要注意的地方
    在MOSS 2007中使用Web Capture这个WebPart
    MOSS 配置SSO的时候可能遇到"您没有执行此操作的权限"的错误
    基于Web的操作系统,是否有可能?
    ASP.NET 中对于异常的处理
    在读取站点地图时自动应用安全设置
    JavaScript中的Window窗口对象【转载】
    当动态TSQL语句遇到除零的问题
  • 原文地址:https://www.cnblogs.com/Paul-Guderian/p/10241735.html
Copyright © 2011-2022 走看看