luogu_P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

Posted ling-zhi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu_P3121 [USACO15FEB]审查(黄金)Censoring (Gold)相关的知识,希望对你有一定的参考价值。

栈模拟,哈希

#include<iostream> 
#include<cstdio>

#define ri register int
#define u unsigned long long

namespace opt {
    
    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<0||s>9) {
            if(s==-) f=-1;
            s=getchar();
        }
        while(s>=0&&s<=9) {
            x=(x<<1)+(x<<3)+s-0;
            s=getchar();
        }
        return x*f;
    }
    
}

using opt::in;

#define NN 500005

#include<cstring>
#include<cmath>
#include<algorithm>

namespace mainstay {
    
    const u bas=131;
    
    char s[NN],t[NN];
    
    u N,sum[NN],pr,ans[NN];
    
    struct node{
        u len,val;
    }a[NN];
    
    inline bool cmp(const node &x,const node &y){
        return x.len<y.len;
    }
        
    u pow[NN];
        
    inline void solve(){
        scanf("%s",s+1);
        scanf("%d",&N);
        for(ri i(1);i<=N;++i) {
            scanf("%s",t+1);
            a[i].len=(std::strlen(t+1));
            for(ri j(1);j<=a[i].len;++j){
                a[i].val=a[i].val*bas+(t[j]-a+1);
            }
        }
        pow[0]=1;
        u len(std::strlen(s+1));
        for(ri i(1);i<=len;++i) pow[i]=pow[i-1]*bas;
        std::sort(a+1,a+N+1,cmp);
        u i(0);
        for(ri i(0);i<=len;){
            ans[++pr]=s[++i]-a+1,sum[pr]=sum[pr-1]*bas+s[i]-a+1;
            while(pr<a[1].len) ans[++pr]=s[++i]-a+1,sum[pr]=sum[pr-1]*bas+s[i]-a+1;
            for(ri j(1);j<=N;++j){
                if(a[j].len<=pr){
                    u _w(sum[pr]-sum[pr-a[j].len]*pow[a[j].len]);
                    if(_w==a[j].val){
                        pr-=a[j].len;
                        break;
                    }
                }
                else break;
            }
        }
        for(ri i(1);i<=pr;++i) printf("%c",ans[i]+a-1);
    }
    
}

int main() {
    
    //freopen("x.txt","r",stdin);
    std::ios::sync_with_stdio(false);
    mainstay::solve();
    
}

 

以上是关于luogu_P3121 [USACO15FEB]审查(黄金)Censoring (Gold)的主要内容,如果未能解决你的问题,请参考以下文章

Luogu P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

洛谷 P3121 [USACO15FEB]审查(黄金)Censoring (Gold) AC自动机+栈

LuoguP3121 [USACO15FEB]审查(黄金)Censoring (Gold)Hash做法By cellur925

[USACO15FEB] Censoring

[USACO15FEB]Censoring S「KMP算法」