栈模拟,哈希
#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(); }
原文地址:https://www.cnblogs.com/ling-zhi/p/11827863.html
时间: 2024-10-09 05:43:50