SCU - 4438——Censor(哈希)

题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438

题意:给出一个串a和串b,串b中如果有a串则删除,删除后b剩下的串再连到一起,再找串a,反复进行,直到不能再删除为止。求最后的串b。

思路:把串a的哈希求出,用栈来维护串b。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#define ULL unsigned long long
using namespace std;
const int maxx=5*1e6+5;
const ULL Hash=131;
char strA[maxx],strB[maxx];
ULL per[maxx],tem[maxx],hb[maxx];
void init()
{
    per[0]=1;
    for(int i=1; i<maxx; i++)
    {
        per[i]=per[i-1]*Hash;
    }
    return ;
}
int main()
{
    init();
    while(~scanf("%s%s",strA+1,strB+1))
    {
        ULL ha=0,r=0;
        hb[1]=0;
        int lenA=strlen(strA+1),lenb=strlen(strB+1);
        for(int i=1;i<=lenA;i++)
        {
            ha=ha*Hash+strA[i]-'a'+1;
        }
        for(int i=1;i<=lenb;i++)
        {
            tem[++r]=strB[i];
            hb[r]=hb[r-1]*Hash+strB[i]-'a'+1;
//            printf("%llu %llu\n",hb[r]-hb[r-lenA]*per[lenA],ha);
            if(r>=lenA&&hb[r]-hb[r-lenA]*per[lenA]==ha)
            {
                r-=lenA;
            }
        }
        for(int i=1;i<=r;i++)
        {
//            printf("%llu ",hb[i]);
            printf("%c",tem[i]);
        }printf("\n");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值