随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 
希望在Str 中删掉 1个屏蔽词(一个屏蔽词可能出现多次)

求最后的串

 

栈+hash

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>
using namespace std;
 const int N =1e6+3;
 #define int long long
int bas=29,pow[N],h[N];
char a[N],b[N];
int n,m, st[N],hh;
 
 int f2(int l,int r){
     return h[r]-h[l-1]*pow[r-l+1];
 }
 signed main(){
    int i,j;
    pow[0]=1;
    for(i=1;i<=1e6;i++) pow[i]=pow[i-1]*bas;
    cin>>a+1>>b+1;
    n=strlen(a+1),m=strlen(b+1);
     
    int vb= 0;
    for(i=1;i<=m;i++)
        vb =vb*bas+b[i];
         
    for(i=1;i<=n;i++){
        st[++hh]=a[i]; h[hh]=h[hh-1]*bas+a[i];
        if(hh-m+1>=1&&f2(hh-m+1,hh)==vb) hh-=m;
    }
    for(i=1;i<=hh;i++) cout<<char(st[i]);
 }

 

posted on   towboat  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示