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 11 string pp.

(1≤length of w,p≤5⋅1061≤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

中文概要:

给出一个word字符串和一个page字符串,要求不断重复把p中所有w串删去.

        输出最终结果(eg: w=abc p=aaabcbc  =>   ans=a    )

#include<queue>  
#include<set>  
#include<map>  
#include<cstdlib>  
#include<cstring>
#include<iostream>  
using namespace std;
typedef unsigned long long ULL;//若哈希结果超出LL的范围可以自动取模LL_max
const int maxn = 5e6 + 50;
char w[maxn], p[maxn], ans[maxn];
struct Hash
{
	ULL hash=131;
	ULL hash_b[maxn];//字符串哈希表(用于在母串中p[i]-p[i-len]*has_b[len]时使用,这时得到的才是该子串的哈希值)
	ULL w_hash;//w串的哈希值 
	ULL w_len;//w串的长度
	ULL p_hash[maxn];//p串的哈希值 
	void init()//初始化
	{
		w_hash=0;//串哈希值初始化
		p_hash[0]=0;
		hash_b[0]=1;//哈希表初始化
		for(int i=1;i<(int)5e6;i++)
		hash_b[i]=hash_b[i-1]*hash;
	}
	bool check(int pos)
	{
		if(pos >= w_len&&p_hash[pos] - p_hash[pos - w_len] * hash_b[w_len] == w_hash)//判断子串是否与母串pos开始len长度串匹配成功
		return true;
		return false;
	}
	void solve()
	{
		w_len=strlen(w);
		for(int i=0;i<w_len;i++)//获得w串的哈希值
		w_hash=w_hash*hash+(w[i]-'a'+1);
		int top=0,p_len=strlen(p);//top为ans串的长度
		for(int i=0;i<p_len;i++)//ans为输出的串,ans从1开始赋值,有与w串相同的就直接删除 
		{
			ans[top++]=p[i];
			p_hash[top]=p_hash[top-1]*hash+(p[i]-'a'+1);
			if(check(top))
			top-=w_len;//边计算哈希值边删 
		}
		for(int i=0;i<top;i++)
		printf("%c",ans[i]);
		printf("\n");
	}
}H;
int main()
{
	while(~scanf("%s%s",&w,&p))
	{
		H.init();
		H.solve();
	}
	return 0;
}

思路:

设一个ans串,在用p串不断给ans串赋值的时候同时来判断是否有与w串相同的,有就直接删除

说实话是第一次看结构体套函数的例子,原来不知道结构体还能套函数,妈的,原来结构体这么吊

感觉本题难点在于判断子串是否与母串pos开始len长度串匹配成功

也就是check函数中if(pos >= w_len&&p_hash[pos] - p_hash[pos - w_len] * hash_b[w_len] == w_hash)

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值