四川第七届 C Censor (字符串哈希)

本文介绍了一种使用哈希算法快速定位并移除文本中的敏感词的方法。通过预处理字符串和运用哈希表,该算法能高效地处理大量数据,特别适用于需要实时过滤大量文本的应用场景。

Censor

frog is now a editor to censor so-called sensitive words (敏感词).

She has a long text pp. Her job is relatively simple -- just to find the first occurence of sensitive word ww and remove it.

frog repeats over and over again. Help her do the tedious work.

Input

The input consists of multiple tests. For each test:

The first line contains 11 string ww. The second line contains 11string pp.

(1length of w,p51061≤length of w,p≤5⋅106, w,pw,p consists of only lowercase letter)

Output

For each test, write 11 string which denotes the censored text.

Sample Input

    abc
    aaabcbc
    b
    bbb
    abc
    ab

Sample Output

    a
    
    ab


题目大意;就是每组测试数据输入两个字符串;问第二个字符串中把第一个字符串一样的删除,问最后面还剩下什么;并输出;注意字符串删除后还会形成一个新的字符串

#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
#define ll unsigned long long//要用长整型
char a[5000005];
char b[5000005];
ll  p[5000005];
ll a131[5000005];
deque<ll>s;
void init()
{//先打表以防超时
    a131[0]=1;
    for(ll i=1;i<=5000000;i++)
    {
        a131[i]=a131[i-1]*131;
    }
}
int main()
{
    init();
    while(~scanf("%s",&a))
    {
        while(!s.empty()) s.pop_back();
        memset(p,0,sizeof(p));
        scanf("%s",&b);
        ll la=strlen(a);
        ll lb=strlen(b);
        ll ss=0;
        for(ll i=0;i<la;i++)
        {//对ss进行哈希
           ss=(ss*131+a[i]);
        }
        ll cnt=1;
        for(ll i=0;i<lb;i++)
        {
            s.push_back(b[i]);
            if(s.size()==1)
            {
                p[cnt++]=b[i];
            }
            else
            {
                p[cnt]=(p[cnt-1]*131+b[i]);
                cnt++;
            }
            if(s.size()>=la&&(p[cnt-1]-(p[cnt-la-1]*a131[la]))==ss)//选择la长度的哈希,要减去之前的
            {//一旦发现哈希相同,删除相同字符串
                for(ll j=1;j<=la;j++)
                {
                    cnt--;
                    s.pop_back();
                }
            }
        }
        while(!s.empty())
        {
           printf("%c",s.front());
           s.pop_front();
        }
        printf("\n");
    }
    return 0;
}

 



转载于:https://www.cnblogs.com/caiyishuai/p/8955069.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值