码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-09 22:01:18      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:long   nod   inline   pen   ace   efi   style   return   scan   

栈模拟,哈希

#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)

标签:long   nod   inline   pen   ace   efi   style   return   scan   

原文地址:https://www.cnblogs.com/ling-zhi/p/11827863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!